前言
本文案例来自业务部门的一个业务场景。他们的业务场景是他们部门研发了一个微服务上下文透传组件,其透传原理也挺简单的,就是通过springboot拦截器把请求参数塞进threadlocal,然后下游通过threadlocal取到值,服务之间进行feign调用时,再把threadlocal的参数塞到header头里面。这个组件一直用得好好的,突然有一天因为传的参数值是中文,导致乱码。他们通过尝试下面的各种方案,都无法解决。最后就让我们部门排查处理。
业务部门的实现思路
他们一开始的思路方向是参数编码不一致导致中文乱码。于是他们就朝这个方向努力着,于是就有了如下方案
方案一:
String value = new String("我是中文乱码".getBytes("ISO-8859-1"),"UTF-8");
这个是常用解决字符串中文乱码的方法之一
方案二:编写字符编码过滤器
@WebFilter(urlPatterns = "/*",filterName = "CharacterEncodingFilter")
public class CharacterEncodingFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
filterChain.doFilter(request , response);
}
@Override
public void destroy() {
}
}
然后启动类上加上@ServletComponentScan。@WebFilter是servlet3.0才有的注解。当然这个过滤器你还可以这么写
public class CharacterEncodingFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
filterChain.doFilter(request , response);
}
@Override
public void destroy() {
}
}
然后启动类上加上@ServletComponentScan。@WebFilter是servlet3.0才有的注解。当然这个过滤器你还可以这么写
public class CharacterEncodingFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
filterChain.doFilter(request , response);
}
@Override
public void destroy() {
}
}
写个bean配置类,如下
@Bean
public FilterRegistrationBean registerAuthFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new CharacterEncodingFilter();
registration.addUrlPatterns("/*");
registration.setName("CharacterEncodingFilter");
registration.setOrder(1);
return registration;
}
方案三:在application.yml指定编码格式为utf-8
spring:
http:
encoding:
charset: utf-8
enabled: true
force: true
server:
tomcat:
uri-encoding: UTF-8
方案四:写个StringHttpMessageConverter
百度来的基本上都是长这样。不过在spring5版本WebMvcConfigurerAdapter这个类已经过时。其替代方式是实现WebMvcConfigurer接口或者继承WebMvcConfigurationSupport。不过如果使用WebMvcConfigurationSupport,则会使springboot的mvc自动装配失效。失效的原因是
拓展一点小知识,加上@EnableWebMvc同样也会springboot的mvc自动装配失效。其原因是
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration这个配置类继承WebMvcConfigurationSupport
介绍那么多种方案,并没有解决按例的问题。那问题点出在哪里?前边案例我们提到过,在feign调用时,会把threadlocal的参数塞到header里面。真正乱码的问题点就在这里,header是不支持中文传输的,如果你硬要传输,基本上接收方接到就是???这种看似乱码的符号
破题关键
在把threadlocal的值塞到header里面时,先做下URLEncoder编码,形如
URLEncoder.encode(“我是中文乱码”,"UTF-8")
在接收header参数时,做下URLDecoder.解码,形如下
URLDecoder.decode(header中待解码的参数值, "UTF-8")
总结
方向错了,虽然再怎么努力看似也啥没卵用,不过至少可能会收获其他意想不到的东西
以上就是springboot参数传中文乱码的解决方案的详细内容,更多关于springboot参数传中文乱码的资料请关注编程网其它相关文章!