In spring boot, there is no problem in any URL, such as localhost:8080/anything, but only in the default URL, such as localhost:8080/ or localhost:8080.
IDE is currently using Intellij.
This is not being done. I tried to grab a breakpoint to try debugging, but I didn't run that method in the first place
During debugging, it was confirmed whether a bean was generated or not, but it was confirmed that a bean was generated without problem.
This is not being done. I tried to grab a breakpoint to try debugging, but I didn't run that method in the first place
During debugging, it was confirmed whether a bean was generated or not, but it was confirmed that a bean was generated without problem.
@Slf4j
@Controller
public class HomeController {
@RequestMapping("/")
public String index(){
return "index";
}
}
It works normally. Whatever URI is set, it works fine.
@Slf4j
@Controller
public class HomeController {
@RequestMapping("/anyting")
public String index(){
return "index";
}
}
My sauce is as follows What should I check?
Thank you in advance for your help.
CodePudding user response:
Try giving this it works:
@Slf4j
@Controller
public class HomeController {
@GetMapping("")
public String index(){
return "index";
}
}
CodePudding user response:
@Slf4j
@Controller
public class HomeController {
@RequestMapping(value="/",method = RequestMethod.GET) //try this
public String index(){
return "index";
}
}
CodePudding user response:
You can try with the below approaches:
1) Use @Controller along with @ResponseBody
@Controller
is used to mark classes as Spring MVC Controller.If you use only @Controller
, you have to specify the @ResponseBody
as well in each API methods.
@Controller
@Slf4j
public class HomeController {
@RequestMapping("/")
@ResponseBody
public String index() {
return "index";
}
@RequestMapping("/anything")
@ResponseBody
public String index2() {
return "index";
}
}
Output: The above code will return the "index" string as a response of this API.
If you want to show index.html page, then you have to modify your code as below and please note your index.html should be placed in resources/static folder.
@RequestMapping("/")
@ResponseBody
public ModelAndView index() {
return new ModelAndView("index.html");
}
Output: This will redirect you to index.html page.
2) Use @RestController
@RestController
annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialised version of @Component annotation.
@RestController
@Slf4j
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/anything")
public String index2() {
return "index";
}
}
Output: The above code will return the "index" string as a response of this API.
If you want to show index.html page, then you have to modify your code as below and please note your index.html should be placed in resources/static folder.
@RequestMapping("/")
public ModelAndView index() {
return new ModelAndView("index.html");
}
Output: This will redirect you to index.html page.
CodePudding user response:
everyone. Thank you for answering my question.
but it didn't have solve on me.
Instead, we will solve it in another way and share it with you.
The first word of @xerx593, I got a hint from telling me to set the logging level to debug.
2022-10-13 16:54:01.710 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/", parameters={}
2022-10-13 16:54:01.712 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ParameterizableViewController [view="forward:/index"]
2022-10-13 16:54:01.724 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2022-10-13 16:54:01.724 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : View name 'forward:', model {}
2022-10-13 16:54:01.725 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to [/index]
2022-10-13 16:54:01.728 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/index", parameters={}
2022-10-13 16:54:01.734 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], classpath [public/], classpath [], classpath [resources/], classpath [META-INF/resources/], classpath [META-INF/resources/webjars/]]
2022-10-13 16:54:01.736 DEBUG 79836 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-10-13 16:54:01.736 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
2022-10-13 16:54:01.740 DEBUG 79836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
(Imports logs into snippet for viewing.)
If you check the log and approach "/", you are forwarding it to "/index".
So I changed the code to receive "/index" and process it.
@Slf4j
@Controller
public class HomeController {
@RequestMapping("/index")
public String index(){
return "index";
}
}
It works for me.
Thank you.