SpringMvc Controller請求鏈接忽略大小寫(包含攔截器)及@ResponseBody返回String中文亂碼處理。。。
@RequestMapping(value = "/tests", method = RequestMethod.POST) @ResponseBody public String tests(HttpServletRequest request){ return "我是"; }
比如我們有這么個請求,返回的是“我是”這么一個中文字符串,請求鏈接是“/tests”,先處理返回中文亂碼問題.
1)我們一般會在springmvc啟動配置文件中配置這么一段StringHttpMessageConverter的轉換器,但即使是配置了也不管用:
<!-- 對包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 --> <context:component-scan base-package="com.web.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <aop:aspectj-autoproxy/> <!-- 開啟@controller注解,同時解決返回中文亂碼問題 --> <mvc:annotation-driven> <mvc:message-converters> <!--<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <!-- 避免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> <property name="features"> <list> <value>WriteMapNullValue</value> <value>DisableCircularReferenceDetect</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
請注意上面這段配置,是先掃包,后啟用SpringMVC驅動器轉換器的配置,這樣配置返回json出現下載鏈接的問題是解決了,但是返回中文字符串亂碼問題並沒有解決,如何解決呢?很簡單,講掃包和驅動器的位置對調一下,配置如下即可:
<!-- 開啟@controller注解,同時解決返回中文亂碼問題 --> <mvc:annotation-driven> <mvc:message-converters> <!--<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <!-- 避免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> <property name="features"> <list> <value>WriteMapNullValue</value> <value>DisableCircularReferenceDetect</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 對包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 --> <context:component-scan base-package="com.web.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <aop:aspectj-autoproxy/>
2)Controller請求鏈接忽略大小寫(攔截器)
網上很多資料是這么配置的,加一個繼承自
WebMvcConfigurationSupport的轉換類,它的作用是將所有@RequestMapping的請求均轉換為小寫,這樣做請求問題是解決了,如上用http://localhost:8080/tets或者http://localhost:8080/Tests都能請求到,但是如果
配置了<mvc:interceptors>攔截器,攔截器是攔截不到的,攔截器就成了瞎子不起作用了,通常配置如下(這段配置攔截器不管用):
@Configuration public class WebConfig extends WebMvcConfigurationSupport { @Override public void configurePathMatch(PathMatchConfigurer configurer) { AntPathMatcher matcher = new AntPathMatcher(); matcher.setCaseSensitive(false); configurer.setPathMatcher(matcher); } }
正確的做法如下,繼承自AntPathMatcher路徑匹配:
public class CaseInsensitivePathMatcher extends AntPathMatcher { protected boolean doMatch(String pattern, String path, boolean fullMatch, Map uriTemplateVariables) { return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables); } }
然后將這個類加入</mvc:annotation-driven>內,如下:
<bean id="caseInsensitivePathMatcher" class="com.web.interceptor.CaseInsensitivePathMatcher"/> <mvc:annotation-driven> <mvc:message-converters> <!--<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <!-- 避免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> <property name="features"> <list> <value>WriteMapNullValue</value> <value>DisableCircularReferenceDetect</value> </list> </property> </bean> </mvc:message-converters> <mvc:path-matching path-matcher="caseInsensitivePathMatcher"/> </mvc:annotation-driven>
所以,如上兩個問題合起來配置如下:
<!-- 開啟@controller注解,同時解決返回中文亂碼問題 --> <bean id="caseInsensitivePathMatcher" class="com.web.interceptor.CaseInsensitivePathMatcher"/> <mvc:annotation-driven> <mvc:message-converters> <!--<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <!-- 避免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> <property name="features"> <list> <value>WriteMapNullValue</value> <value>DisableCircularReferenceDetect</value> </list> </property> </bean> </mvc:message-converters> <mvc:path-matching path-matcher="caseInsensitivePathMatcher"/> </mvc:annotation-driven> <!--<mvc:annotation-driven> </mvc:annotation-driven>--> <!-- 對包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 --> <context:component-scan base-package="com.web.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:properties/*.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean> <aop:aspectj-autoproxy/>