'html file not returning in the Rest API and return the file name only like 'index' in the screen for the following code when used @RestController
html file not returning in the Rest API and return the file name only like 'index' for the following code when used @RestController and working fine only for @Controller. Here I am using spring boot REST application and index.html with bootstrap
Solution 1:[1]
If you know REST web services then you must know the fundamental difference between a REST API and a web application i.e. the response from a web application is generally view (HTML + CSS) because they are intended for human viewers.
REST API just returns data in form of JSON or XML because most of the REST clients are programs. This difference is also obvious in the @Controller and @RestController annotation.
@RestController = @Controller + @ResponseBody
When you use @Controller annotation then it corresponds to MVC workflow in Spring Boot and it renders the view. The key difference is that you do not need to use @ResponseBody on each and every handler method once you annotate the class with @RestController.
@ResponseBody is a Spring annotation which binds a method return value to the web response body. It is not interpreted as a view name. It uses HTTP Message converters to convert the return value to HTTP response body, based on the content-type in the request HTTP header. The content type can be JSON or XML.
Solution 2:[2]
It is the expected behaviour of @RestController. The main difference between @RestController and @Controller is @RestController will by-pass the view resolution but @Controller will not.
Technically , it is due to the presence of the @ResponseBody of the @RestController. @ResponseBody will enable RequestResponseBodyMethodProcessor to process the result return from the @RestController method and it will mark that the request has been fully handled within the controller method and by-pass the view resolution process (see this).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ALS |
| Solution 2 |
