HttpMessageConverter<T> & @RequestBody & ResponseBody & HttpEntity & ResponseEntity
1.HttpMessageConverter<T> ----------------->接口
①简介
* Strategy interface that specifies a converter that can convert from and to HTTP requests and responses.
HttpMessageConverter<T> 是 Spring3.0 新添加的一个接口,负责将请求信息转换为一个对象(类型为 T),或者将对象( 类型为 T)输出为响应信息
②HttpMessageConverter<T>的常用实现类
DispatcherServlet 默认装配 RequestMappingHandlerAdapter ,而 RequestMappingHandlerAdapter 默认装配如下 HttpMessageConverter
2.@RequestBody & ResponseBody
Talk is cheap! Show me your code!
ResponseBody
////////////////@ResponseBody/////////////////////////////
@ResponseBody
@RequestMapping("testRequestBody")
public String testRequestBody(){
return "abc";
}
////////////////////////@RequestBody///////////////////////////
@RequestMapping("testRequestBody")
public String testRequestBody(@RequestBody String body){
System.out.println(body);
return "SUCCESS";
}
3.HttpEntity & ResponseEntity
////////////HttpEntity/////////////// 将请求报文转化为String类型对象
@RequestMapping("testHttpEntity")
public String testHttpEntity(HttpEntity<String> entity){
System.out.println(entity.getHeaders().getContentLength());
return "a";
}
///////////////ResponseEntity///////////////////// 体现出的就是下载的感觉,就是将服务器的资源回传给客户端
@RequestMapping("testResponseEntity")
public ResponseEntity<String> testResponseEntity(){
ResponseEntity<String> responseEntity = new ResponseEntity<String>("hello world",
HttpStatus.OK);
return responseEntity;
}