Spring 中的 @RequestBody 和 @ResponseBody 注解 [英] @RequestBody and @ResponseBody annotations in Spring

查看:22
本文介绍了Spring 中的 @RequestBody 和 @ResponseBody 注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 Spring 3 中的 @RequestBody@ResponseBody 注释?它们是为了什么?任何例子都会很棒.

Can someone explain the @RequestBody and @ResponseBody annotations in Spring 3? What are they for? Any examples would be great.

推荐答案

文档中有一个完整的部分叫做 16.3.3.4 使用@RequestBody 注释映射请求正文.还有一个叫做 16.3.3.5 使用@ResponseBody注解映射响应体.我建议你咨询这些部分.同样相关:@RequestBody javadocs, @ResponseBody javadocs

There is a whole Section in the docs called 16.3.3.4 Mapping the request body with the @RequestBody annotation. And one called 16.3.3.5 Mapping the response body with the @ResponseBody annotation. I suggest you consult those sections. Also relevant: @RequestBody javadocs, @ResponseBody javadocs

使用示例如下:

使用像 JQuery 这样的 JavaScript 库,你会像这样发布一个 JSON 对象:

Using a JavaScript-library like JQuery, you would post a JSON-Object like this:

{ "firstName" : "Elmer", "lastName" : "Fudd" }

您的控制器方法如下所示:

Your controller method would look like this:

// controller
@ResponseBody @RequestMapping("/description")
public Description getDescription(@RequestBody UserStats stats){
    return new Description(stats.getFirstName() + " " + stats.getLastname() + " hates wacky wabbits");
}

// domain / value objects
public class UserStats{
    private String firstName;
    private String lastName;
    // + getters, setters
}
public class Description{
    private String description;
    // + getters, setters, constructor
}

现在,如果您的类路径上有 Jackson(并且有一个 设置),Spring 会将传入的 JSON 转换为来自帖子正文的 UserStats 对象(因为您添加了 @RequestBody 注释)并将返回的对象序列化为 JSON(因为您添加了 @ResponseBody 注释).所以浏览器/客户端会看到这个 JSON 结果:

Now if you have Jackson on your classpath (and have an <mvc:annotation-driven> setup), Spring would convert the incoming JSON to a UserStats object from the post body (because you added the @RequestBody annotation) and it would serialize the returned object to JSON (because you added the @ResponseBody annotation). So the Browser / Client would see this JSON result:

{ "description" : "Elmer Fudd hates wacky wabbits" }

有关完整的工作示例,请参阅我之前的答案:https://stackoverflow.com/a/5908632/342852

See this previous answer of mine for a complete working example: https://stackoverflow.com/a/5908632/342852

注意:RequestBody/ResponseBody 当然不限于 JSON,两者都可以处理多种格式,包括纯文本和 XML,但 JSON 可能是最常用的格式.

Note: RequestBody / ResponseBody is of course not limited to JSON, both can handle multiple formats, including plain text and XML, but JSON is probably the most used format.

从 Spring 4.x 开始,您通常不会在方法级别使用 @ResponseBody,而是在类级别使用 @RestController,效果相同.

Ever since Spring 4.x, you usually won't use @ResponseBody on method level, but rather @RestController on class level, with the same effect.

这里引用了官方Spring MVC 文档:

@RestController 是一个 组合注解本身是元注解@Controller@ResponseBody 表示控制器每个方法都继承了类型级别的 @ResponseBody 注释,并且,因此,直接写入响应正文与视图分辨率并使用 HTML 模板进行渲染.

@RestController is a composed annotation that is itself meta-annotated with @Controller and @ResponseBody to indicate a controller whose every method inherits the type-level @ResponseBody annotation and, therefore, writes directly to the response body versus view resolution and rendering with an HTML template.

这篇关于Spring 中的 @RequestBody 和 @ResponseBody 注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆