Spring reference写到:
Mapping the response body with the @ResponseBody annotationJast领会到:
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.
使用@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会转化成不同的 类型。
来源:oschina
链接:https://my.oschina.net/u/219009/blog/286513