官术网_书友最值得收藏!

Using @ResponseBody annotation

This section represents the concepts related to the usage of the @ResponseBody annotation for returning a response to the client request.

The @ResponseBody annotation can be applied both at the class level and the method level. When @ResponseBody is applied at the class level along with the @Controller annotation, another annotation such as @RestController can be used instead.

The @ResonseBody annotation represents the fact that the value returned by the method will form the body of the response. When the value returned is an object, the object is converted into an appropriate JSON or XML format by HttpMessageConverters. The format is decided based on the value of  the produce attribute of the @RequestMapping annotation, and also the type of content that the client accepts. Take a look at the following example:

    @Controller
public class RestDemoController {

@RequestMapping(value="/hello", method=RequestMethod.POST, produces="application/json")
@ResponseBody
public HelloMessage getHelloMessage(@RequestBody User user) {
HelloMessage helloMessage = new HelloMessage();
String name = user.getName();
helloMessage.setMessage( "Hello " + name + "! How are you doing?");
helloMessage.setName(name);
return helloMessage;
}
}
主站蜘蛛池模板: 北京市| 闻喜县| 永春县| 绥阳县| 新密市| 高邑县| 漾濞| 惠东县| 汾西县| 正安县| 石嘴山市| 赤峰市| 岳池县| 阿巴嘎旗| 施秉县| 鄂尔多斯市| 攀枝花市| 疏附县| 高安市| 渭南市| 晋江市| 诸城市| 卢氏县| 红原县| 延安市| 漾濞| 保亭| 泽州县| 浦东新区| 高密市| 济阳县| 青冈县| 祁连县| 句容市| 商都县| 紫云| 福泉市| 武汉市| 石台县| 温州市| 外汇|