Reputation: 406
I try to call a Spring Boot controller from a Ajax Request:
$('#female').click(function(){
$('#analysisTable').DataTable( {
"ajax": '/analyse/female'
});
});
The Idea behind this is to load a list into a js datatable. The controller looks like:
@GetMapping("/analyse/female")
public List<GenderAnalysis> analysisByFemale(final Model model) {
final List<GenderAnalysis> result = analyseDao.getAnalysisByGender(AnalyseDAO.Gender.Female);
return result;
}
The controller works fine. But i get an Thymeleaf Template error. Every response will be handled through a ThymeleafLayoutInterceptor and load "normal" (not ajax) requests into a template.
The erorr is the following:
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/analyse/female.html]")
I know that there is not a female.html resource and i even won't have one. Just serve the raw List to ajax call.
I'm not sure how to work with Spring Boot+Thymeleaf+Ajax even with templates. Could it be a handling problem with the interceptor? What can i do? Anybody able to help?
Upvotes: 0
Views: 1171
Reputation: 20477
If you want to return JSON rather than a Thymeleaf template, you should either:
1) Declare the controller as a @RestController
rather than just a @Controller
. This will affect all @GetMapping
, @PostMapping
and @RequestMapping
annotations on your controller class.
or
2) Declare the method as a @ResponseBody
in addition to the @GetMapping
.
@GetMapping("/analyse/female")
@ResponseBody
public List<GenderAnalysis> analysisByFemale() {
return analyseDao.getAnalysisByGender(AnalyseDAO.Gender.Female);
}
Upvotes: 2