1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【Spring reference】@ResponseBody注解

【Spring reference】@ResponseBody注解

时间:2022-04-21 09:18:36

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。