欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

【Spring reference】@ResponseBody注解

发布时间:2025/1/21 javascript 138 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【Spring reference】@ResponseBody注解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

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







转载于:https://my.oschina.net/jast90/blog/286513

总结

以上是生活随笔为你收集整理的【Spring reference】@ResponseBody注解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。