Skip to content
Advertisement

Getting resource not found when try to return html page from controller

I am unable to render html page in springboot. Here is code…

JavaScript

but whenever i hit http://localhost:8080/home it shows following logs

JavaScript

Advertisement

Answer

Avoid @RestController for MVC Based Application which has to return a view. It is mainly used for REST APIs. While @Controller can return a view

More On @RestController:

enter image description here

  • This annotation is a specialized version of @Controller which adds @Controller and @ResponseBody annotation automatically. so we do not have to add @ResponseBody to our mapping methods. That means @ResponseBody is default active.
  • If you use @RestController you cannot return a view (By using Viewresolver in Spring/Spring-Boot)
  • @RestController also converts the response to JSON/XML automatically as @ResponseBody makes the returned objects to something that could be in the body, e.g. JSON or XML

Controller vs RestController


JavaScript

Project Structure enter image description here

No need to specify spring.mvc.view.prefix=/WEB-INF/html/ spring.mvc.view.suffix=.html

Also, make sure you don’t have any additional class with @EnableWebMvc annotation. This can mess up the spring-boot autoconfiguration.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement