Java 解析模板[]时出错,模板可能不存在或任何已配置的模板解析程序都无法访问该模板

Java 解析模板[]时出错,模板可能不存在或任何已配置的模板解析程序都无法访问该模板,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,下面是我的控制器。如您所见,我想返回htmltype @Controller public class HtmlController { @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE) public Employee html() { return Employee.builder() .name("test") .build(); } } 我得到了以下错误: Circ

下面是我的控制器。如您所见,我想返回
html
type

@Controller
public class HtmlController {

  @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
  public Employee html() {
    return Employee.builder()
      .name("test")
      .build();
  }
}

我得到了以下错误:

Circular view path [login]: would dispatch back to the current handler URL [/login] again

我通过跟帖把它修好了

现在我得到另一个错误:

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
有人能帮我为什么我必须依赖ThymalLeaf来提供html内容以及为什么我会遇到这个错误吗

为什么我必须依靠Thymeleaf来提供HTML内容

你没有。您可以用其他方式来做,只需告诉Spring您正在做的事情,即告诉它返回值是响应本身,而不是用于生成响应的视图的名称

正如政府所说:

下表描述了支持的控制器方法返回值

  • String
    :使用
    viewsolver
    实现解析并与隐式模型一起使用的视图名称 — 通过命令对象和
    @modeldattribute
    方法确定。handler方法还可以通过声明
    model
    参数以编程方式丰富模型

  • @ResponseBody
    :返回值通过
    HttpMessageConverter
    实现转换并写入响应。看

  • 任何其他返回值:与此表中任何早期值不匹配的任何返回值[…]都被视为视图名称(通过
    requesttoviewmanamestranslator选择的默认视图名称适用)

在您的代码中,
Employee
对象如何转换为
text/html
?现在,代码属于“任何其他返回值”类别,但失败了

你可以,例如

  • 使用Thymeleaf模板(推荐使用此方法):

    这实际上使用了一个内置的

  • 使用已注册的(不推荐):

    @GetMapping(path=“/”,products=MediaType.TEXT\u HTML\u值)
    @应答器//
    
    @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
    }
    
    @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
    }
    
    @GetMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
    @ResponseBody // <== added annotation
    public Employee html() {
        return Employee.builder()
            .name("test")
            .build();
    }