Reputation: 7556
Below is my controller. As you see I want to return html
type
@Controller
public class HtmlController {
@GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
public Employee html() {
return Employee.builder()
.name("test")
.build();
}
}
I initial got following error:
Circular view path [login]: would dispatch back to the current handler URL [/login] again
I fixed it up by following this post.
Now I am getting another error:
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [], template might not exist or might not be accessible by any of the configured Template Resolvers
Can someone help me with why I have to rely on thymleaf for serving a html content and why am I getting this error.
Upvotes: 0
Views: 4757
Reputation: 159260
why I have to rely on Thymeleaf for serving HTML content
You don't. You can do it in other ways, you just have to tell Spring that that's what you're doing, i.e. by telling it that the return value is the response itself, not the name of the view using to generate the response.
As the Spring documentation says:
The next table describes the supported controller method return values.
String
: A view name to be resolved withViewResolver
implementations and used together with the implicit model — determined through command objects and@ModelAttribute
methods. The handler method can also programmatically enrich the model by declaring aModel
argument.
@ResponseBody
: The return value is converted throughHttpMessageConverter
implementations and written to the response. See@ResponseBody
....
Any other return value: Any return value that does not match any of the earlier values in this table [...] is treated as a view name (default view name selection through
RequestToViewNameTranslator
applies).
In your code, how does an Employee
object get turned into text/html
? Right now, the code falls into the "Any other return value" category, and that fails.
You could, e.g.
Use a Thymeleaf template (this is the recommended way):
@GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
public String html(Model model) { // <== changed return type, added parameter
Employee employee = Employee.builder()
.name("test")
.build();
model.addAttribute("employee", employee);
return "employeedetail"; // view name, aka template base name
}
Add a String toHtml()
method to Employee
and then do:
@GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody // <== added annotation
public String html() { // <== changed return type (HTML is text, i.e. a String)
return Employee.builder()
.name("test")
.build()
.toHtml(); // <== added call to build HTML content
}
This actually uses a built-in StringHttpMessageConverter
.
Use a registered HttpMessageConverter
(not recommended):
@GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody // <== added annotation
public Employee html() {
return Employee.builder()
.name("test")
.build();
}
This of course requires that you write a HttpMessageConverter<Employee>
implementation that supports text/html
, and register it with the Spring Framework.
Upvotes: 1