有没有办法禁用 Thymeleaf,或仅启用某些 REST 调用?

use*_*663 9 java rest spring spring-mvc thymeleaf

例如,我有一个基本的 POST,它使用 Thymeleaf 返回一个名为“result”的 html。这很有效而且很酷。

@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting) {
    return "result";
}
Run Code Online (Sandbox Code Playgroud)

但我有另一个完全不相关的方法,它执行不同的操作,并且不返回模板。

@PostMapping(value = "/otherstuff", headers = "content-type=multipart/*")
public Object otherStuff(@RequestParam("file") MultipartFile dataFile) {
    // Totally unrelated stuff
    return resultList;
}
Run Code Online (Sandbox Code Playgroud)

当然,我得到一个例外:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/otherstuff", template might not exist or might not be accessible by any of the configured Template Resolvers 
Run Code Online (Sandbox Code Playgroud)

因为我故意不解析模板。我可以关闭此方法的 ThymeLeaf 吗?我的 Rest API 是多用途的,如果 ThymeLeaf 最终破坏了整个项目,那将毫无帮助。

谢谢。

Mah*_*zad 10

正如评论中所述,您应该@ResponseBody在您的方法上使用注释。
这就是你所需要的。