Spring中的@RequestBody和@ResponseBody注解

2022-01-13 00:00:00 annotations spring java spring-mvc

有人能解释一下 Spring 3 中的 @RequestBody@ResponseBody 注释吗?它们是干什么用的?任何例子都会很棒.

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

推荐答案

文档中有一个完整的Section,叫做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(并且有一个 <mvc:annotation-driven> setup),Spring 会将传入的 JSON 转换为来自 post body 的 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.

相关文章