【Spring reference】@ResponseBody注解

心已入冬 提交于 2019-11-27 11:09:50

Spring reference写到:

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会转化成不同的 类型。







标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!