How to Return a HTML Page from a Restful Controller in Spring Boot

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

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.

Return HTML page from Spring controller

Okey so apparently Spring Boot supports this without any additional configuration or controllers.

All I had to do was to place the HTML file in the correct directory /resources/static/some/path/test.html and it can be reached at localhost:8080/some/path/test.html.

In my attempts to change the directory from which the file is served I was unsuccessful. It seems that providing a separate @EnableWebMvc (needed for configuring the resource handlers) breaks the Spring Boot configuration. But I can live with using the default /static directory.

Is there a way to return HTML page from Restful Controller in Spring Boot?

First of all, in your code in the controller method uploadMultipleFiles, you are actually calling the controller method uploadFile directly by treating it just like another method which is not ideal. The fact that you need to recursively call another controller endpoint means that it is a design flaw in itself. So , try to remove this bit of code by providing the logic in service layer to handle this scenario .

Secondly , splitting the view controller and the rest controller into two separate classes is the right approach. The @RestController annotation is designed to serve json response by default that is why we have the @Controller annotation which serves both model and view. The response code 403 that you receive has nothing to do with this.

Not able to return to html page from springboot controller

I think your controller is not able to find your homepage. I am not sure but please first check your pom.xml file to whether you have added Thymeleaf dependency ie.

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

And when you return to an HTML page from the controller it looks for the HTML page in the temples folder, So be sure that your homepage.html is within the templets folder and then you can return your page name as a string from your controller
Like this:

@Controller
public class HomeController {

@RequestMapping ("/")
public String homePage() {
return homepage;
}
}

Once try this it will work.

Cannot return html when using post method in spring boot

Use return "redirect:/success.html" so that you can get your html pages. Also from our chat, please use thymeleaf which is a template engine.
You can add it by adding it in your dependencies in the pom.xml file. This is what you should add inside your dependecies tag in pom.xml.

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>


Related Topics



Leave a reply



Submit