HttpMessageConverter

在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,HttpMessageConverter完成了这种消息转换机制。

HttpMessageConverte接口定义:

  1. package org.springframework.http.converter;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import org.springframework.http.HttpInputMessage;
  5. import org.springframework.http.HttpOutputMessage;
  6. import org.springframework.http.MediaType;
  7. public interface HttpMessageConverter<T> {
  8. boolean canRead(Class<?> clazz, MediaType mediaType);
  9. boolean canWrite(Class<?> clazz, MediaType mediaType);
  10. List<MediaType> getSupportedMediaTypes();
  11. T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
  12. throws IOException, HttpMessageNotReadableException;
  13. void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
  14. throws IOException, HttpMessageNotWritableException;
  15. }

HttpMessageConverter接口的定义出现了成对的canRead(),read()和canWrite(),write()方法,MediaType是对请求的Media Type属性的封装。举个例子,当我们声明了下面这个处理方法。

  1. @RequestMapping(value="/string", method=RequestMethod.POST)
  2. public @ResponseBody String readString(@RequestBody String string) {
  3. return "Read string '" + string + "'";
  4. }

在SpringMVC进入readString方法前,会根据@RequestBody注解选择适当的HttpMessageConverter实现类来将请求参数解析到String变量中,具体来说是使用了StringHttpMessageConverter类,它的canRead()方法返回true,然后它的read()方法会从请求中读出请求参数,绑定到readString()方法的string变量中。

当SpringMVC执行readString方法后,由于返回值标识了@ResponseBody,SpringMVC将使用StringHttpMessageConverter的write()方法,将结果作为String值写入响应报文,当然,此时canWrite()方法返回true。

将上述过程集中描述的一个类是org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor,这个类同时实现了HandlerMethodArgumentResolver和HandlerMethodReturnValueHandler两个接口。前者是将请求报文绑定到处理方法形参的策略接口,后者则是对处理方法返回值进行处理的策略接口。RequestResponseBodyMethodProcessor这个类,同时充当了方法参数解析和返回值处理两种角色。而在此过程中,以是否有@RequestBody和@ResponseBody为条件,然后分别调用HttpMessageConverter来进行消息的读写。

ContentNegotiatingViewResolver

主要完成同一资源,多种展现的功能。

三种指定资源格式的方式

Http Request Header: Accept
  1. GET /test/123 HTTP/1.1
  2. Accept: application/json //返回json格式数据
  3. GET /test/123 HTTP/1.1
  4. Accept: application/xml //返回xml格式数据

如果你的资源是通过浏览器访问的,那么由于浏览器的差异,传递到服务器的Accept Header是有差异的,将导致服务器不知道返回何种格式的数据给浏览器。下面是各种浏览器的Accept Header:

  1. chrome:
  2. Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
  3. firefox:
  4. Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. IE8:
  6. Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
使用扩展名
  1. /test/123.xml //返回xml格式数据
  2. /test/123.json //返回json格式数据

丧失了同一url多种展现的方式,但现在这种在实际环境中是使用最多的,因为更加符合程序员的审美观。

使用参数
  1. /test/123?format=json //返回json格式数据
  2. /test/123?format=xml //返回xml格式数据

现在很多open API是使用这种方式,但可能由于要编写的字符较多,所以较少使用。

ContentNegotiatingViewResolver配置

内容协商(content negotiation)的工作是由ContentNegotiatingViewResolver来完成的。它的工作模式支持我上面讲的三种,ContentNegotiatingViewResolver是根据客户提交的MimeType(如 text/html,application/xml)来跟服务端的一组viewResover的MimeType相比较,如果符合,即返回viewResover的数据。

ContentNegotiatingViewResolver配置:

  1. <bean id="contentNegotiationManager"
  2. class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  3. <property name="favorPathExtension" value="true" />
  4. <property name="favorParameter" value="true" />
  5. <property name="parameterName" value="format" />
  6. <property name="ignoreAcceptHeader" value="false" />
  7. <property name="mediaTypes">
  8. <value>
  9. json=application/json
  10. xml=application/xml
  11. </value>
  12. </property>
  13. <property name="defaultContentType" value="text/html" />
  14. </bean>
  15. <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  16. <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
  17. <property name="viewResolvers">
  18. <list>
  19. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
  20. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  21. <property name="prefix" value="/WEB-INF/jsp/"/>
  22. <property name="suffix" value=".jsp"/>
  23. </bean>
  24. </list>
  25. </property>
  26. <property name="defaultViews">
  27. <list>
  28. <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
  29. </list>
  30. </property>
  31. </bean>

HttpMessageConverter和ContentNegotiatingViewResolver的更多相关文章

  1. 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务

    原文地址:http://www.ibm.com/developerworks/cn/web/wa-restful/ 简介: Spring,构建 Java™ 平台和 Enterprise Edition ...

  2. HttpMessageConverter(消息转换器 )和@responsebody使用(转)

    @responsebody表示该方法的返回结果直接写入HTTP response body中 一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@resp ...

  3. 08点睛Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)

    8.1 配置 Spring MVC的配置是通过继承WebMvcConfigurerAdapter类并重载其方法实现的; 前几个教程已做了得配置包括 01点睛Spring MVC 4.1-搭建环境 配置 ...

  4. springmvc<一>一种资源返回多种形式【ContentNegotiatingViewResolver】

    restful服务中一个重要的特性就是一种资源可以有多种表现形式,在springmvc中可以使用ContentNegotiatingViewResolver这个视图解析器来实现这种方式. 描述资源的三 ...

  5. no suitable HttpMessageConverter found for request type [java.lang.Integer]

    今天在使用Spring Template的时候遇到了这个异常: no suitable HttpMessageConverter found for request type [java.lang.I ...

  6. SpringMVC 中HttpMessageConverter简介和Http请求415 Unsupported Media Type的问题

    一.概述: 本文介绍且记录如何解决在SpringMVC 中遇到415 Unsupported Media Type 的问题,并且顺便介绍Spring MVC的HTTP请求信息转换器HttpMessag ...

  7. HttpMessageConverter用法

    HttpMessageConverter接口定义 * Strategy interface that specifies a converter that can convert from and t ...

  8. Spring ContentNegotiatingViewResolver

    1. Spring 返回视图采用了ViewResolver,如果一般是jsp的话,可以采用InternalResourceViewResolver. 2.还可以通过ContentNegotiating ...

  9. 将SpringMVC中的HttpMessageConverter替换为Gson

    读者们看到这个标题也许会感到奇怪,SpringMVC中默认的HttpMessageConverter不是Jackson吗,但是我在使用的过程中发现Jackson并不好用,如果有一些复杂的嵌套类型,当然 ...

随机推荐

  1. Linux下使用locale命令设置语言环境

    locale命令设置语言环境 在Linux中通过locale来设置程序运行的不同语言环境,locale由 ANSI C提供支持.locale的命名规则为_.,如zh_CN.GBK,zh代表中文, CN ...

  2. JEECG获取当前登录人的值

    TSUser user = ResourceUtil.getSessionUserName(); mv.addObject("fbillerid", user.getUserNam ...

  3. CMapStringToPtr添加与释放

    // 创建MapStringToPtr CMapStringToPtr m_prjFiles; CStringList m_fileList; m_fileList.AddTail(_T(" ...

  4. .NET的URL重写

    [概述] URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程.重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了 ...

  5. 用活Firewalld防火墙之direct

    原文地址:http://www.excelib.com/article/294/show 学生在前面已经给大家介绍过了Firewalld中direct的作用,使用他可以直接使用iptables.ip6 ...

  6. H5测试 有空了解下里面没有用过的东西

  7. canvas之画矩形

    <canvas id="canvas" width="600" height="500" style="background ...

  8. 浅谈PHP面向对象编程(七、抽象类与接口)

    7.0 抽象类与接口 当定义一个类时,常常需要定义一些方法来描述该类的行为特征.但有时这些方法的实现方式是无法确定的,此时就可以使用抽象类和接口. 抽象类和接口用于提高程序的灵活性.抽象类是一种特殊的 ...

  9. 排除maven jar冲突 maven tomcat插件启动报错 filter转换异常

    最近在搞一个ssm+shiro的整合 用的maven tomcat插件 启动的时候报错,提示 maven org.springframework.web.filter.CharacterEncodin ...

  10. linux rz 乱码

    Linux shell rz和sz是终端下常用的文件传输命令,rz和sz通过shell被调用,其中rz用于从启用终端的系统上传文件到目标系统(终端登录的目标系统), 这里不过多介绍这些命令,只是记录一 ...