Search code examples
springrestweb-serviceswebthymeleaf

ThymeLeaf: Cannot display view after calling endpoint


I am trying to call a REST endpoint and then display a ThymeLeaf template:

The Endpoint:

@GetMapping("/devices")
public String getDeviceDetailU(Model model) {
  List<FinalDevice> devices = deviceService.getAll();
  model.addAttribute("devices", devices);
  return "deviceList";  
}

For the endpoint I tried returning /deviceList, /deviceList.html, deviceList.html.

Whenever I navigate to the endpoint, I simply get the string that was returned.

Here is the template:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <body>
Hello World!
    </body>
</html>

While I understand, at this point, it will not display the list, I just want to be forwarded to the template.

If I go to localhost:8080/deviceType I display that template. This to me indicates it is not a security or configuration issue.

Any ideas?

This should all work according to this tutorial.


Solution

  • You probably have @RestController instead of just a @Controller.

    If you want templates to be rendered you need to use @Controller. @RestController means that all your @Mappings simply serialize the return value and output it as json or xml (which is why you are seeing the string deviceList instead of the template).