問(wèn)題描述
有人能解釋一下 Spring 3 中的 @RequestBody
和 @ResponseBody
注釋嗎?它們是干什么用的?任何例子都會(huì)很棒.
Can someone explain the @RequestBody
and @ResponseBody
annotations in Spring 3? What are they for? Any examples would be great.
推薦答案
文檔中有一個(gè)完整的Section,叫做16.3.3.4 用@RequestBody注解映射請(qǐng)求體.還有一個(gè)叫做 16.3.3.5 使用@ResponseBody 注解映射響應(yīng)體.我建議你查閱這些部分.也相關(guān):@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 庫(kù),您可以像這樣發(fā)布 JSON 對(duì)象:
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
}
現(xiàn)在,如果您的類路徑中有 Jackson(并且有一個(gè) <mvc:annotation-driven>
setup),Spring 會(huì)將傳入的 JSON 轉(zhuǎn)換為來(lái)自 post body 的 UserStats 對(duì)象(因?yàn)槟砑恿?@RequestBody
注釋)并將返回的對(duì)象序列化為 JSON(因?yàn)槟砑恿?@ResponseBody
注釋).所以瀏覽器/客戶端會(huì)看到這個(gè) JSON 結(jié)果:
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" }
有關(guān)完整的工作示例,請(qǐng)參閱我以前的答案:https://stackoverflow.com/a/5908632/342852一個(gè)>
See this previous answer of mine for a complete working example: https://stackoverflow.com/a/5908632/342852
注意:RequestBody/ResponseBody 當(dāng)然不限于 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 開(kāi)始,您通常不會(huì)在方法級(jí)別使用 @ResponseBody
,而是在類級(jí)別使用 @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
是一個(gè) 組合注釋 本身就是 元注釋用 @Controller
和 @ResponseBody
表示一個(gè)控制器每個(gè)方法都繼承了類型級(jí)別的 @ResponseBody
注釋,并且,因此,直接寫入響應(yīng)正文而不是視圖解析并使用 HTML 模板進(jìn)行渲染.
@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.
這篇關(guān)于Spring中的@RequestBody和@ResponseBody注解的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!