SpringMVC ResponseBody返回字符串(JSON)乱码

    xiaoxiao2022-06-23  20

    SpringMVC的@ResponseBody注解可以将请求方法返回的对象直接转换成JSON对象,但是当返回值是String的时候,中文会乱码,原因是因为其中字符串转换和对象转换用的是两个转换器,而String的转换器中固定了转换编码为”ISO-8859-1”,网上也很多种解决方法,有通过配置Bean编码的,也有自己重写转换器。

    有效的解决方法是:

    在spring-mvc.xml中配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="com.baobaotao.controller" /> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="ignoreAcceptHeader" value="false"/> <property name="defaultContentType" value="text/html"/> <property name="mediaTypes"> <map> <entry key="html" value="text/html;charset=UTF-8"/> <entry key="json" value="application/json;charset=UTF-8"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="10"/> <!--use jsp view resolver--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="100"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </list> </property> </bean>

    注意以上配置需要放到之前,否则无效。

    看上面的配置 有时候会出现 ‘mvc:annotation-driven’ must have no character or element问题,就是不能有子元素。 原因是要mvc 3.0以上,其实你不用指定,默认用最新的

    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"

    如果还不行替换xml文件头部:

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    针对乱码问题网上普遍的两种解决方法是: 1.1.在action中取得response,由他写入响应数据。

    response.setHeader("Cache-Control", "no-cache"); response.setContentType("text/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.write(result);

    这样每次都要写很麻烦

    2.方法一: 自己继承AbstractHttpMessageConverter,写一个类复制 StringHttpMessageConverter.java的代码,将 public static final Charset DEFAULT_CHARSET = Charset.forName(“ISO-8859-1”); 改为 public static final Charset DEFAULT_CHARSET = Charset.forName(“UTF-8”); spring-servlet的配置文件如下

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="com.renren001.converter.UTF8StringHttpMessageConverter" /> </list> </property> </bean>
    转载请注明原文地址: https://ju.6miu.com/read-1123180.html

    最新回复(0)