如何解决springboot参数传中文乱码

如何解决springboot参数传中文乱码

前言

本文案例来自业务部门的一个业务场景。他们的业务场景是他们部门研发了一个微服务上下文透传组件,其透传原理也挺简单的,就是通过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() {
}

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