简体   繁体   English

Restful Web服务中的Spring @RequestBody注释

[英]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. 感谢@RestController我不需要添加注释@ResposneBody ,因为spring知道它是rest控制器,并且他不会生成视图,而是会返回json对象。

Unfortunately there is one more annotation related to this topic. 不幸的是,还有一个与此主题相关的注释。 It is @RequestBody , when controller method accept json object as a parameter. 它是@RequestBody ,当控制器方法接受json对象作为参数时。 And it will have to be pointed before that parameter. 并且必须在该参数之前指出。

My question is there a way to get rid of that annotation ( @RequestBody ).? 我的问题是有办法摆脱那个注释( @RequestBody )。 If my controller is rest controller ( @RestController instead of regular @Controller ) it should be demanded from spring? 如果我的控制器是休息控制器( @RestController而不是常规的@Controller ),应该从spring请求它?

No, you'll have to specify @RequestBody . 不,你必须指定@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. Java方法只能有一个返回值,因此@ResponseBody是明确的,但有多种可能的方法可以解释映射控制器参数(特别是,使用带表单编码的@ModelAttribute@RequestBody一个非常常见的替代方法使用JSON),您需要告诉Spring如何映射传入的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM