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
Finding Repeated Words on a String and Counting the Repetitions
How to Set Environment Variable or System Property in Spring Tests
Java Coding Converted to Pseudo Code
How to Convert Minutes to Hours and Minutes (Hh:Mm) in Java
Use String.Split() With Multiple Delimiters
How to Display Pdf Inside Webview in Android
Get Current Week Start and End Date in Java - (Monday to Sunday)
Convert Multi If-Else to Simple Lambda Expression Code in Java
How to Do Sorting in Array List Without Using Collections in Java
How to Check If a Date Is Within a Certain Range
How to Trim White Space from All Elements in Array
Could Not Extract Resultset in Hibernate
Java.Lang.Illegalargumentexception: Not a Managed Type: Class
How to Use Jackson to Deserialise an Array of Objects
How to Convert Date Which I Got from Firebase Server as Am Getting Error Date