SpringMVC 中HttpMessageConverter简介和Http请求415 的问题
一.问题描述:
在SprinvMVC的Web程序中,我在页面发送Ajax的POST请求,然后在服务器端利用@requestBody接收请求body中的参数,当时运行过程中,我想服务器发送Ajax请求,浏览器一直反馈415UnsupportedMediaType或者400的状态码,以为是Ajax写的有问题。便查找了半天资料,才发现spring-mvc.config文件的配置中少了东西,当然也有可能是你真的在Ajax中缺少了对Content-Type参数的设置。分析后应该是我springMVC-config.xml文件配置有问题。
(注):400:(错误请求)服务器不理解请求的语法。415:(不支持的媒体类型)请求的格式不受请求页面的支持。
二.解决方法:
在springMVC-config.xml文件中,增加了一个StringHttpMessageConverter请求信息转换器,配置片段如下:
<!---StringHttpMessageConverterbean--> <beanid="stringHttpMessageConverter"class="org.springframework.http.converter.StringHttpMessageConverter"/> <!--启动SpringMVC的注解功能,完成请求和注解POJO的映射--> <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <propertyname="messageConverters"> <list> <refbean="mappingJacksonHttpMessageConverter"/> <!--新增的StringMessageConverterbean--> <refbean="stringHttpMessageConverter"/> <refbean="jsonHttpMessageConverter"/> <refbean="formHttpMessageConverter"/> </list> </property> </bean>
三.HttpMessageConverter请求信息转换器简介:
HttpMessageConverter接口指定了一个可以把Httprequest信息和Httpresponse信息进行格式转换的转换器。通常实现HttpMessageConverter接口的转换器有以下几种:
ByteArrayHttpMessageConverter:负责读取二进制格式的数据和写出二进制格式的数据;
StringHttpMessageConverter: 负责读取字符串格式的数据和写出二进制格式的数据;
ResourceHttpMessageConverter:负责读取资源文件和写出资源文件数据;
FormHttpMessageConverter: 负责读取form提交的数据(能读取的数据格式为application/x-www-form-urlencoded,不能读取multipart/form-data格式数据);负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;
MappingJacksonHttpMessageConverter: 负责读取和写入json格式的数据;
SourceHttpMessageConverter: 负责读取和写入xml中javax.xml.transform.Source定义的数据;
Jaxb2RootElementHttpMessageConverter: 负责读取和写入xml标签格式的数据;
AtomFeedHttpMessageConverter: 负责读取和写入Atom格式的数据;
RssChannelHttpMessageConverter: 负责读取和写入RSS格式的数据;
更多关于HttpMessageConverter的信息请看:
http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/http/converter/HttpMessageConverter.html
四.HttpMessageConverter请求信息转换器执行流程:
当用户发送请求后,@Requestbody注解会读取请求body中的数据,默认的请求转换器HttpMessageConverter通过获取请求头Header中的Content-Type来确认请求头的数据格式,从而来为请求数据适配合适的转换器。例如contentType:applicatin/json,那么转换器会适配MappingJacksonHttpMessageConverter。响应时候的时候同理,@Responsebody注解会启用HttpMessageConverter,通过检测Header中Accept属性来适配的响应的转换器。
总结:
当在使用SpringMVC做服务器数据接收时,尤其是在做Ajax请求的时候,尤其要注意contentType属性,和accepte属性的设置,在springmvc-config.xml中配置好相应的转换器。当我们在用SpringMVC做Ajax请求的时候,有的做法用response.getWriter().print()的方法,还有更好的方法就是添加@Responsebody注解,直接返回Map类型的数据,转换器自动转换为JSON数据类型。