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

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;
}
}
主站蜘蛛池模板: 铁力市| 横山县| 烟台市| 景洪市| 兴和县| 平度市| 施甸县| 峨边| 枣阳市| 长寿区| 塘沽区| 灌云县| 繁昌县| 响水县| 遂川县| 阳新县| 鹤岗市| 南京市| 庆安县| 开原市| 棋牌| 苏尼特右旗| 通化县| 嘉祥县| 赣榆县| 昌黎县| 和政县| 祁阳县| 历史| 南充市| 新宁县| 家居| 平武县| 朔州市| 汉源县| 二连浩特市| 鄂尔多斯市| 略阳县| 汝州市| 萨嘎县| 阿尔山市|