V
V
Vadim Raksha2017-10-25 23:45:41
Java
Vadim Raksha, 2017-10-25 23:45:41

Spring boot: Post request without returning template?

I am using Thymeleaf library
I have a POST function:

@PostMapping("/add2Cart")
void addToCart(@RequestBody String str, HttpServletRequest request) {
        ...
}

JSON is passed from it with an Ajax request, but after the function is executed, an error occurs:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "add2Cart", template might not exist or might not be accessible by any of the configured Template Resolvers

Is it possible to process a post request without returning a template?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-10-26
@hasdfa

In essence, it is enough to mark the method with the @ResponseBody annotation so that the returned value is treated not as a template name, but as a response:

@PostMapping("/add2Cart")
@ResponseBody
void addToCart(@RequestBody String str, HttpServletRequest request) {
        ...
}

Judging by the terms and prepositions you use, you have a very vague idea of ​​what is really going on. First, addToCart is not a function, but a method. Secondly, you pass an ajax request to it, and json is returned from it. The next question is correct: "Is it possible to process a post request without returning a template?"
I don't understand the meaning of your last question. What exactly do you want? Where do you need to pass the java object to the method?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question