springMVC返回汉字字符串乱码,以及返回的字符串乱码的问题
1、springMVC在使用@ResponseBody注解返回字符串为什么出现乱码呢?(这里以spring4.3.1为例)
原因分析:原因在返回字符串时StringHttpMessageConverter默认编码为:ISO8859-1,如下:
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); private final List<Charset> availableCharsets; private boolean writeAcceptCharset = true; /** * A default constructor that uses {@code "ISO-8859-1"} as the default charset. * @see #StringHttpMessageConverter(Charset) */ public StringHttpMessageConverter() { this(DEFAULT_CHARSET); } /** * A constructor accepting a default charset to use if the requested content * type does not specify one. */ public StringHttpMessageConverter(Charset defaultCharset) { super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL); this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); } //.........以下省略 }
找到问题之后,可以从以下几种方式解决乱码问题:
方式一、在springMVC配置文件中修改StringHttpMessageConverter的默认编码:(已经验证有效)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <!-- spring消息转换器:如果要动用这些消息转换器,需要在特定的位置加上@RequestBody或者@ResponseBody --> <property name="messageConverters"> <list> <!-- 作用:String类型解析器,允许直接返回String类型的消息 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <!-- constructor-arg配置可作为以后扩展了解 --> <!-- <constructor-arg value="UTF-8" index="0"/> --> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <!-- 注意:用以下时不会起作用 --> <!-- <value>text/html;charset=UTF-8</value> --> </list> </property> </bean> </list> </property> </bean>
方式二、直接在springMVC配置文件中直接配置<mvc:annotation-driven />,在对应的方法上如下:
@RequestMapping(value="beanParameter",produces="text/html;charset=UTF-8") @ResponseBody public String getPamaterbyEntity(Employee employee){
System.out.println("empName:"+employee.getEmpName()+",salary:"+employee.getSalary()); ModelAndView modelAndView = new ModelAndView(); return employee.getEmpName(); }
方式三、未验证
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter"/> <ref bean="jackson2HttpMessageConverter"/> </list> </property> </bean> <mvc:annotation-driven />
注意:配置必须在 <mvc:annotation-driven /> 之前,否则将不会启效;<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter。
方式四、直接使用json解析器(未验证)
<!-- 返回json 需要导入jackson-core-2.4.1.jar,jackson-databind-2.4.1.jar,jackson-annotations-2.4.0.jar包 -->
<mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 --> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
注意:使用@responsebody注解并且返回值类型为String时,返回的string字符串带有双引号,其原因是直接将string类型转成了json字符串,
应该在json解析器之前添加字符串解析器
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>