前端选择表单提交,后端接收带下划线参数转为驼峰实体类过滤器

文章目录

  • 前言
  • 一、配置类
  • 二、过滤器
  • 三、包装器


前言

情况是这样的,原来的后端是php,现在要改为java,原来参数传递的规则是全部使用下划线参数,post请求使用jackson当然一个注解就搞定了,get请求就比较繁琐了,这是我的解决的方法,通过给req设置一个包装器来完成实现的.


一、配置类

@Configuration
public class FilterConfig{ 
	@Bean
    public FilterRegistrationBean<CustomRequestFilter> customRequestFilter() {
        FilterRegistrationBean<CustomRequestFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CustomRequestFilter());
        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        registration.addUrlPatterns("/*");
        return registration;
    }
}

二、过滤器


import cn.hutool.core.util.StrUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class CustomRequestFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        Map<String, String[]> modifiedParameters = new HashMap<>(request.getParameterMap());

        // 在这里对modifiedParameters进行修改,以支持不同类型的参数映射
        Map<String, String[]> newMap = modifiedParameters.entrySet().stream()
                .collect(Collectors.toMap(
                        k -> StrUtil.toCamelCase(k.getKey()),
                        entry -> entry.getValue().clone() // 使用clone()方法来创建副本
                ));

        // 创建包装了修改后参数的自定义请求对象
        CustomServletRequestWrapper requestWrapper = new CustomServletRequestWrapper(request, newMap);

        // 继续处理过滤链
        filterChain.doFilter(requestWrapper, servletResponse);
    }
}

三、包装器


import cn.hutool.core.util.StrUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.Map;

public class CustomServletRequestWrapper extends HttpServletRequestWrapper {

    private final Map<String, String[]> modifiedParameters;

    public CustomServletRequestWrapper(HttpServletRequest request, Map<String, String[]> modifiedParameters) {
        super(request);
        this.modifiedParameters = modifiedParameters;
    }

    @Override
    public String getParameter(String name) {
        String[] values = modifiedParameters.get(name);
        if (values != null && values.length > 0) {
            return values[0];
        }
        return super.getParameter(name);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return modifiedParameters;
    }

    @Override
    public String[] getParameterValues(String name) {
        return modifiedParameters.get(StrUtil.toCamelCase(name));
    }

    @Override
    public Enumeration<String> getParameterNames() {
        Enumeration<String> parameterNames = super.getParameterNames();
        return new Enumeration<String>() {
            @Override
            public boolean hasMoreElements() {
                return parameterNames.hasMoreElements();
            }

            @Override
            public String nextElement() {
                return StrUtil.toCamelCase(parameterNames.nextElement());
            }
        };
    }
}

前端使用? & 拼接一整个对象表单提交,后端也可以通过驼峰命名实体类来接受参数,如前端传递
ip:port/aa/bb?cc_dd=ee&ff_gg=hh后端可以用

class AaBb{
 private String ccDd;
 private String ffGg;
}

你可能感兴趣的:(spring,boot)