masterdany88
masterdany88

Reputation: 5341

Spring @RequestBody annotation in Restful web service

Thanks to @RestController I don't need to add annotation @ResposneBody, cause spring knows that it is rest controller, and he will not generate view, but instead it will return json object.

Unfortunately there is one more annotation related to this topic. It is @RequestBody, when controller method accept json object as a parameter. And it will have to be pointed before that parameter.

My question is there a way to get rid of that annotation (@RequestBody).? If my controller is rest controller (@RestController instead of regular @Controller) it should be demanded from spring?

Upvotes: 3

Views: 1707

Answers (1)

No, you'll have to specify @RequestBody. A Java method can have only a single return value, and so the @ResponseBody is unambiguous, but there are multiple possible ways that mapped controller parameters might be interpreted (in particular, using @ModelAttribute with form encoding is a very common alternative to @RequestBody with JSON), and you'll need to tell Spring how to map the incoming request.

Upvotes: 2

Related Questions