可以通过自定义一个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<PropertyValue> pvs = mpvs.getPropertyValueList();
List<PropertyValue> 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);
}
}
...阅读原文