springMVC解決中文亂碼問題
阿新 • • 發佈:2018-12-05
1.配置Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" metadata-complete="true" version="3.1"> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.配置springmvc的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" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置SpringMVC --> <!-- 1.開啟SpringMVC註解模式 --> <!-- 簡化配置: (1)自動註冊DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter (2)提供一些列:資料繫結,數字和日期的format @NumberFormat, @DateTimeFormat, xml,json預設讀寫支援 --> <mvc:annotation-driven/> <!-- 返回json 需要匯入 jackson-annotations.jar,jackson-core.jar,jackson-databind.jar--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <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> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html; charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" /> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="objectMapper" /> <property name="targetMethod" value="configure" /> <property name="arguments"> <list> <value type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value> <value>false</value> </list> </property> </bean> <!-- 2.靜態資源預設servlet配置 (1)加入對靜態資源的處理:js,gif,png (2)允許使用"/"做整體對映 --> <mvc:default-servlet-handler/> <!-- 4.掃描web相關的bean --> <context:component-scan base-package="com.tang.*"/> </beans>
3.編寫測試類
package com.tang.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * @Auther tang * @Date 2018/11/30 * @Description */ @Controller public class TestSpringMVC { @ResponseBody @RequestMapping(value = "/getString",method = RequestMethod.GET,produces = "application/json;charset=UTF-8") public String getTestString(){ return "hehe坑什麼呢?自從我spring-web加了介面卡的資訊轉換器之後,再加上produces = application/json;charset=UTF-8就不會亂碼了"; } }
總結:springMVC解決中文亂碼,只需要在配置檔案的介面卡(RequestMappingHandlerAdapter)裡面配置資訊轉換器(MappingJackson2HttpMessageConverter),然後再請求方法上新增(produces = "application/json;charset=UTF-8")即可。