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/>
相关文章
相关标签/搜索