Skip to content
Advertisement

Is there a way to disable Thymeleaf, or only enable for certain REST calls?

For instance, I have a basic POST that returns an html called “result” using Thymeleaf. This works and is cool.

@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting) {
    return "result";
}

But I have another totally unrelated method, that does something different, and returns not a template.

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

Naturally, I get an exception:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/otherstuff", template might not exist or might not be accessible by any of the configured Template Resolvers 

because I’m intentionally not resolving a template. Can I turn off ThymeLeaf for this method? My Rest API is multi-purpose, and it would be rather unhelpful if ThymeLeaf ends up disrupting the whole project.

Thanks

Advertisement

Answer

Just to provide a separate answer for this question.

As stated in the comments, you should use @ResponseBody annotation on your method.
That’s all you need.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement