SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理
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/>
SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理的更多相关文章
- SpringMVC 使用@ResponseBody返回json 中文乱码与返回实体类报错
有时候我们发现接收的是中文,返回却是个?.这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 /** * Implementation of {@link Htt ...
- SpringMVC 使用@ResponseBody返回json 中文乱码
这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3. ...
- SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...
- SpringMVC(四)-- 文件下载、自定义拦截器、异常处理
1.文件下载 用ResponseEntity<byte[]> 返回值完成文件下载 具体参见本博客之前的<文件上传下载> @RequestMapping(value=" ...
- SpringMVC拦截器和@ResponseBody注解冲突
在使用@ResponseBody注解后controller方法只会返回ModelandView对象的数据模型,不会返回视图,这样有很多好处,但是如果在拦截器中进行了页面转发,在满足页面转发条件时,不会 ...
- 如何解决http请求返回结果中文乱码
如何解决http请求返回结果中文乱码 1.问题描述 http请求中,请求的结果集中包含中文,最终以乱码展示. 2.问题的本质 乱码的本质是服务端返回的字符集编码与客户端的编码方式不一致. 场景的如服务 ...
- 解决springmvc使用ResponseBody注解返回json中文乱码问题
spring版本:4.2.5.RELEASE 查看“org.springframework.http.converter.StringHttpMessageConverter”源码,中有一段说明: B ...
- springmvc学习笔记二:重定向,拦截器,参数绑定
Controller方法返回值 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. 返回void 在Contro ...
- SpringMVC框架学习笔记(6)——拦截器
SpringMVC拦截器需要实现接口HandlerInterceptor 有3个方法,分别在请求处理前.请求处理后和在DispatcherServlet处理后执行 实现代码: package inte ...
随机推荐
- CSS复合选择器
复合选择器是由两个或多个基础选择器,通过不同的方式组合而成的,目的是为了可以选择更准确更精细的目标元素标签. 交集选择器 交集选择器由两个选择器构成,其中第一个为标签选择器,第二个为class选择器, ...
- 洛谷 P1149 火柴棒等式
嗯.... 这道题好讨厌啊!!!! 一开始莫名RE,然后发现数组小了,然后发现后面几个点总是WA,原来推的少了.... 并且这道题的思路真的好水啊!! 先看一下题: 题目描述 给你n根 ...
- nginx关闭默认站点/空主机头(禁止IP直接访问、防止域名恶意解析)
监控时做了负载均衡,所以只能让nginx指定域名访问,那我们就可以防止因为域名不对跳到默认的页面去. curl -I -H “host:域名” --include https://19 ...
- elasticsearch head 连接不到elasticsearch
配置好head后看到没有正常连接到elasticsearch. 重启后效果:
- Android中include标签的使用(打开引用布局,隐藏当前布局)
在开发app的时候,有时候一个布局会反复用到,可以把反复用到的布局单独写一个xml文件,什么时候用到就用includ标签引入xml 下面是我写的反复用到的一个xml,里面有2个button,一个Tex ...
- 可持久化Treap 赛前摸鱼笔记
1.基本结构 随机化工具 unsigned int SEED = 19260817; //+1s inline int Rand(){ SEED=SEED*1103515245+12345; retu ...
- HDU - 1085 母函数
年轻人的第一道母函数入门题 #include<bits/stdc++.h> using namespace std; const int maxn = 1000+2000+5000+1; ...
- springboot(七)-系列功能配置
热部署 我们程序员在开发web项目的时候,避免不了需要将项目放到tomcat或者其他web容器中运行测试,而所有的程序员都有个习惯,从来都是以debug模式启动的(就好像谁不是这样启动就不是优秀的程序 ...
- filezilla绑定编辑器
编辑 - >设置 ->文件格式关联 ->输入需要关联的编辑器的路径
- Oracle KEEP的用法
[摘录自] http://blog.itpub.net/12932950/viewspace-687036/ http://flyfx.iteye.com/blog/1994993 聚合函数MIN, ...