【Spring reference】@ResponseBody註解

Spring reference寫到: html

Mapping the response body with the @ResponseBody annotation

The @ResponseBodyannotation is similar to @RequestBody. This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).
For example:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
    return "Hello World";
}
 
The above example will result in the text Hello World  being written to the HTTP response stream. As with @RequestBody, Spring converts the returned object to a response body by using an HttpMessageConverter. For more information on these converters, see the previous section and Message Converters.
Jast領會到:


使用@ResponseBody註解映射響應體
@ResponseBody註解和@RequestBody註解相似。這個註解能使用在方法上,用來指明該方法的返回類型將直接做爲響應給Http請求(不會換成Model,或解析爲視圖的名字)

舉個例子:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
    return "Hello World";
}
這上面的例子將以text/html形式響應HTTP請求。和@RequestBody同樣,Spring使用HttpMessageConverter來將方法的返回值進行類型轉換的。對於不一樣的返回值Spring會轉化成不一樣的 類型。
相關文章
相關標籤/搜索