使用Springboot自定义转换器实现参数去空格功能

时间:2022-06-15 08:40:30

自定义转换器实现参数去空格

1.自定义转换器类

实现Converter<S, T>类,重写convert()方法,直接上代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 自定义转换器
* 去掉前后空格
* @author liuy
* @version 2018年11月13日
*/
public class CustomConverter implements Converter<String, String>{
 @Override
 public String convert(String source) {
  try {
   if(source != null){
    source = source.trim();
    if(!"".equals(source)){
     return source;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }
}

2.将转换器交给spring容器处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class SpringdataConvert {
 @Autowired
 private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
 
 @PostConstruct
 public void addConversionConfig(){
  ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter.getWebBindingInitializer();
  if(initializer.getConversionService()!=null){
   GenericConversionService genericConversionService =(GenericConversionService) initializer.getConversionService();
   genericConversionService.addConverter(new CustomConverter());
  }
 }
}

SpringBoot请求参数过滤空格

1、参数修改SpaceHttpServletRequestWrapper

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class SpaceHttpServletRequestWrapper extends HttpServletRequestWrapper {
    public SpaceHttpServletRequestWrapper(HttpServletRequest servletRequest) {
        super(servletRequest);
    }
    @Override
    public String[] getParameterValues(String parameter) {
        String[] values = super.getParameterValues(parameter);
        if (values == null) {
            return new String[0];
        }
        int count = values.length;
        String[] encodedValues = new String[count];
        for (int i = 0; i < count; i++) {
            encodedValues[i] = values[i].trim();
        }
        return encodedValues;
    }
    @Override
    public String getParameter(String parameter) {
        String value = super.getParameter(parameter);
        if (value == null) {
            return null;
        }
        return value.trim();
    }
}

2、空格过滤器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class SpaceFilter implements Filter {
    private FilterConfig filterConfig;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws
            IOException, ServletException {
        filterChain.doFilter(new SpaceHttpServletRequestWrapper(
                (HttpServletRequest) servletRequest), servletResponse);
    }
    @Override
    public void destroy() {
        this.filterConfig = null;
    }
}

3、过滤器初始化

?
1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Bean
    public FilterRegistrationBean spaceFilter() {
        FilterRegistrationBean fitler = new FilterRegistrationBean();
        fitler.setFilter(new SpaceFilter());
        fitler.addUrlPatterns("/*");
        fitler.setName("SpaceFilter");
        fitler.setDispatcherTypes(DispatcherType.REQUEST);
        return fitler;
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_41270375/article/details/84029562