java - How to return a html page from a restful controller in spring boot?

ID : 131319

viewed : 7

Tags : javahtmlspringspring-bootcontrollerjava

Top 5 Answer for java - How to return a html page from a restful controller in spring boot?

vote vote

99

When using @RestController like this:

@RestController public class HomeController {      @RequestMapping("/")     public String welcome() {         return "login";     } } 

This is the same as you do like this in a normal controller:

@Controller public class HomeController {      @RequestMapping("/")     @ResponseBody     public String welcome() {         return "login";     } } 

Using @ResponseBody returns return "login"; as a String object. Any object you return will be attached as payload in the HTTP body as JSON.

This is why you are getting just login in the response.

vote vote

86

Follow below steps:

  1. Must put the html files in resources/templates/

  2. Replace the @RestController with @Controller

  3. Remove if you are using any view resolvers.

  4. Your controller method should return file name of view without extension like return "index"

  5. Include the below dependencies:

    <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-devtools</artifactId> </dependency>` 
vote vote

78

You can try using ModelAndView:

@RequestMapping("/") public ModelAndView index () {     ModelAndView modelAndView = new ModelAndView();     modelAndView.setViewName("index");     return modelAndView; } 
vote vote

62

@kukkuz pretty much answered to the question 'why?'. For those who are still looking into 'how', accumulating what others have answered.

Using a RestController:

@RestController public class MyRestController {     @RequestMapping("/")     public ModelAndView welcome() {         ModelAndView modelAndView = new ModelAndView();         modelAndView.setViewName("login.html");         return modelAndView;     }   } 
  • pay attention that view name is: 'login.html' (full file name).
  • also it is important where the file is located, by default login.html must be in resources/static or resources/public

You may set up an application parameter for a default suffix like:

 spring.mvc.view.suffix=.html 

in this case view name must be without extension like 'login'.

Some suggested thymeleaf can be used. means you have in your pom.xml dependencies something like this:

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-thymeleaf</artifactId>   <version>2.4.4</version> </dependency> 

In that scenario login.html by default must sit in: resources/templates folder, and the call is similar only difference now is in a view name, since .html is used by tymeleaf as a default value.

//  fails by default //  NO fail if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html //  NO fail if thymeleaf is added, and there is a file login.html in a resources/templates folder. @RequestMapping("/loginTest") public ModelAndView loginTest () {     ModelAndView modelAndView = new ModelAndView();     modelAndView.setViewName("login");     return modelAndView; } 

Using a Controller:

@Controller public class MyController {    //gets html from a default 'resources/public' or 'resources/static' folder   @RequestMapping(path="/welcome")   public String getWelcomePage(){       return "login.html";   }    //gets html from a default 'resources/public' or 'resources/static' folder   @RequestMapping("/welcome1")   public ModelAndView getWelcomePageAsModel() {       ModelAndView modelAndView = new ModelAndView();       modelAndView.setViewName("login.html");       return modelAndView;   }    //  fails with 404 resource not found by default   //  NO fail, if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html   //  NO fail, if thymeleaf is added, and there is a file login.html in a resources/templates folder   @RequestMapping(path="/welcome2")   public String thisFails(){       return "login";   } } 
vote vote

57

Replace @Restcontroller with @controller. @Restcontroller returns only content not html and jsp pages.

Top 3 video Explaining java - How to return a html page from a restful controller in spring boot?

Related QUESTION?