SpringMvc Controller請求連接忽略大小寫(包含攔截器)及@ResponseBody返回String中文亂碼處理

SpringMvc Controller請求連接忽略大小寫(包含攔截器)及@ResponseBody返回String中文亂碼處理。。。html

@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出現下載連接的問題是解決了,可是返回中文字符串亂碼問題並無解決,如何解決呢?很簡單,講掃包和驅動器的位置對調一下,配置以下便可:web

  

    <!-- 開啓@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請求連接忽略大小寫(攔截器)spring

    網上不少資料是這麼配置的,加一個繼承自express

WebMvcConfigurationSupport的轉換類,它的做用是將全部@RequestMapping的請求均轉換爲小寫,這樣作請求問題是解決了,如上用http://localhost:8080/tets或者http://localhost:8080/Tests都能請求到,可是若是

配置了<mvc:interceptors>攔截器,攔截器是攔截不到的,攔截器就成了瞎子不起做用了,一般配置以下(這段配置攔截器無論用)json

    

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}

    正確的作法以下,繼承自AntPathMatcher路徑匹配:mvc

   

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>內,以下:app

<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>

 

 

因此,如上兩個問題合起來配置以下:ide

  

<!-- 開啓@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/>
相關文章
相關標籤/搜索