解决@ResponseBody返回字符串中文json对象??问题

我们都知道通过在web.xml中添加拦截器并放置在第一位置来处理提交数据中的中文问题,如下:

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

那么对于返回数据springmvc通过@ResponseBody来将字符串转换为json对象,而其中的中文同样可能会出现中文??

项目中关于返回值的编码原同事的处理代码如下:

在springmvc-servlet.xml中添加配置信息:

<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
<mvc:annotation-driven />
然后添加对字符串和json的字符编码集覆盖默认配置

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value> text/html;charset=UTF-8 </value>
</list>
</property>
</bean>
<!-- JSON格式支持 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value> text/html;charset=UTF-8 </value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>

</bean>

我们的返回数据为:

return "{\"result\":false,\"message\":\"请输入您要删除的合同号!\"}";
年前我们一直都可以用,然后年底客户发现展示信息全部变为???了。

对此的解决方案经过多方查找,三种方法:

最简单麻烦的是第一种,即在每一方法请求中添加:

@RequestMapping(value="/list"produces = "text/html;charset=UTF-8")  红色字体部分即可。

第二种,将配置解析字符集信息和

<mvc:annotation-driven />交换位置,并将过时的AnnotationMethodHandlerAdapter替换为可用的
RequestMappingHandlerAdapter;

最后结果为:

<!--  启动Spring MVC的注解功能,完成请求和注解POJO的映射  -->
   <bean class ="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
       <property name="messageConverters">
           <list>
               <bean class = "org.springframework.http.converter.StringHttpMessageConverter">
                   <property name = "supportedMediaTypes">
                       <list>
                           <value>text/html;charset=UTF-8</value>
                           <value>application/json;charset=UTF-8</value>
                       </list>
                   </property>
               </bean>
               <!-- JSON格式支持 -->
               <bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                   <property name = "supportedMediaTypes">
                       <list>
                           <value>text/html;charset=UTF-8</value>
                       </list>
                   </property>
               </bean>
           </list>
       </property>
   </bean>
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
<mvc:annotation-driven />
第二种方法为对annotation-driven的修改,当然这也的话,我们需要先升级spring-aop-3.0.xsd等相关的配置,如果你的是默认生成请升级到4.0及以上

然后我们的配置信息为:

<bean id="stringHttpMessageConverter"
      class="org.springframework.http.converter.StringHttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>text/plain;charset=UTF-8</value>
            <value>application/json;charset=UTF-8</value>
        </list>
    </property>
</bean>
<!-- Jackson 是Spring 自带的功能 -->
<bean id="mappingJackson2HttpMessageConverter"
      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
          
            <value>text/html;charset=UTF-8</value>
            
        </list>
    </property>
</bean>
<mvc:annotation-driven>
    <mvc:message-converters>
        <ref bean="stringHttpMessageConverter" />
        <ref bean="mappingJackson2HttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>
这样也可以,建议使用方法2来完成修改,从而解决中文???问题。

猜你喜欢

转载自blog.csdn.net/wu1226419614/article/details/79356213
今日推荐