springboot 把接收到请求参数名为下划线格式换成驼峰体

需求:

有时候接收到请求参数名为下划线类型 例如支付宝的支付回调格式为

http://payment.gmall.com:8089/alipay/callback/return?charset=utf-8&out_trade_no=gmall1911291404122322623

我一般喜欢把请求参数用一个实体接收,而我的这个实体的属性名都是驼峰体

这是可以通过自定义一个dataBinder 然后替换默认的dataBinder来实现

首先先定义一个dataBinder

package com.danbro.gmall.common.utils.dataBinder;

import com.danbro.gmall.common.utils.util.StringTransferUtil;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.web.bind.ServletRequestDataBinder;

import javax.servlet.ServletRequest;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname CustomServletRequestDataBinder
 * @Description TODO
 * @Date 2019/11/29 15:39
 * @Author Danrbo
 */
public class CustomServletRequestDataBinder extends ServletRequestDataBinder {

    private final Pattern underLinePattern = Pattern.compile("_(\\w)");

    public CustomServletRequestDataBinder(final Object target) {
        super(target);
    }

    /** 遍历请求参数对象 把请求参数的名转换成驼峰体
     * 重写addBindValues绑定数值的方法
     * @param mpvs 请求参数列表
     * @param request 请求
     */
    @Override
    protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
        List pvs = mpvs.getPropertyValueList();
        List adds = new LinkedList<>();
        for (PropertyValue pv : pvs) {
            String name = pv.getName();
            String camel = this.underLineToCamel(name);
            if (!name.equals(camel)) {
                adds.add(new PropertyValue(camel, pv.getValue()));
            }
        }
        pvs.addAll(adds);
    }

    /**
     * 把app_id转换成appId
     * @param value 要转换的下划线字符串
     * @return 驼峰体字符串
     */
    private String underLineToCamel(final String value) {
        StringBuffer sb = new StringBuffer();
        Matcher matcher = underLinePattern.matcher(value);
        while (matcher.find()){
            matcher.appendReplacement(sb,matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
        
    }
}

替换dataBinder

package com.danbro.gmall.common.utils.processor;

import com.danbro.gmall.common.utils.dataBinder.CustomServletRequestDataBinder;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor;

import javax.servlet.ServletRequest;

/**
 * @Classname CustomServletModelAttributeMethodProcessor
 * @Description TODO 自定义属性处理器
 * @Date 2019/11/29 15:33
 * @Author Danrbo
 */
public class CustomServletModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {
    /**
     * Class constructor.
     *
     * @param annotationNotRequired if "true", non-simple method arguments and
     *                              return values are considered model attributes with or without a
     *                              {@code @ModelAttribute} annotation
     */
    public CustomServletModelAttributeMethodProcessor(boolean annotationNotRequired) {
        super(annotationNotRequired);
    }

    @Override
    protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
        ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
        Assert.state(servletRequest != null,"No servletRequest");
        ServletRequestDataBinder servletRequestDataBinder = (ServletRequestDataBinder) binder;
        //更换请求参数绑定器
        new CustomServletRequestDataBinder(servletRequestDataBinder.getTarget()).bind(servletRequest);

    }
}

 

你可能感兴趣的:(springboot 把接收到请求参数名为下划线格式换成驼峰体)