SpringMCV @ResponseBody 返回乱码问题

@ResponseBody 将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。

<mvc:annotation-driven  />

开启了之后它给AnnotationMethodHandlerAdapter
初始化 7个转换器,

可以通过调用AnnotationMethodHandlerAdapter类的getMessageConverts()
方法来获取转换器的一个集合 List<HttpMessageConverter>

默认给AnnotationMethodHandlerAdapter初始化的有(当然我们也可以添加自定义的converter)

ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter<T>
XmlAwareFormHttpMessageConverter
Jaxb2RootElementHttpMessageConverter
MappingJacksonHttpMessageConverter

客户端的请求header中寻找客户端可接收的类型

  • StringHttpMessageConverter
    支持String , Accept所有类型

  • MappingJacksonHttpMessageConverter
    支持Map List 实体对象等等 ,Accept:application/json

解决办法

spring-mvc-3.0(包括)之前<mvc:annotation-driven/>下面不包含任何子元素

<mvc:annotation-driven>
   <mvc:message-converters register-defaults="true">
     <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
        <property name="supportedMediaTypes">  
             <list>  
                <value>text/html;charset=UTF-8</value>  
             </list>  
       </property>  
     </bean>
     <bean class="org.springframework.http.converter.StringHttpMessageConverter">
       <property>
          <list>
              <value>text/html;charset=UTF-8</value>
          </list>
       </property>
      </bean>
    <mvc:message-converters>
<mvc:annotation-driven>

MappingJackson2HttpMessageConverter 可以替换FastJsonHttpMessageConverter代替

<bean id="fastJsonHttpMessageConverter"  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
        <property name="supportedMediaTypes">  
            <list>  
                <value>application/json;charset=UTF-8</value>  
                <value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->  
            </list>  
        </property>  
        <property name="features">  
            <util:list>  
                <!-- <value>WriteMapNullValue</value> -->  
                <value>QuoteFieldNames</value>  
                <value>WriteDateUseDateFormat</value>  
            </util:list>  
        </property>  
    </bean>  

spring-mvc

  1. spring-mvc-3.1.xsd annotation-driven
xsd:element name="annotation-driven">
<xsd:annotation>...</xsd:annotation>
    <xsd:complexType>
      <xsd:all minOccurs="0">
          <xsd:element name="message-converters" minOccurs="0">...</xsd:element>
          <xsd:element name="argument-resolvers" minOccurs="0">...</xsd:element>
          <xsd:element name="return-value-handlers" minOccurs="0">...</xsd:element>
      </xsd:all>
      <xsd:attribute name="conversion-service" type="xsd:string">...</xsd:attribute>
      <xsd:attribute name="validator" type="xsd:string">...</xsd:attribute>
      <xsd:attribute name="message-codes-resolver" type="xsd:string">...</xsd:attribute>
      <xsd:attribute name="ignoreDefaultModelOnRedirect" type="xsd:boolean">...</xsd:attribute>
    </xsd:complexType>
</xsd:element>
  1. spring-mvc-3.0.xsd annotation-driven
<xsd:element name="annotation-driven">
<xsd:annotation>...</xsd:annotation>
  <xsd:complexType>
    <xsd:attribute name="conversion-service" type="xsd:string">...</xsd:attribute>
    <xsd:attribute name="validator" type="xsd:string">...        </xsd:attribute>
  </xsd:complexType>
</xsd:element>

<xsd:element> XSD元素
<xsd:attribute> XSD属性
<all> 指示器规定子元素可以按照任意顺序出现,且每个子元素必须只出现一次
<maxOccurs> 指示器可规定某个元素可出现的最大次数:
<minOccurs> 指示器可规定某个元素能够出现的最小次数:

最常用的类型是:type

xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容