Springboot Http 요청 매개 변수 바 인 딩 밑줄 회전 낙타 봉 이름

15504 단어 자바
1. ModelAttributeMethodProcessor 사용자 정의
public class CustomServletModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {

    public CustomServletModelAttributeMethodProcessor(final 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 servletBinder = (ServletRequestDataBinder) binder;
        /**
         * ServletModelAttributeMethodProcessor     servletBinder.bind(servletRequest)
         *          ServletRequestDataBinder     CustomServletRequestDataBinder
         */
        new CustomServletRequestDataBinder(servletBinder.getTarget()).bind(servletRequest);
    }

}

ServletRequestDataBinder\# bind 방법 코드 를 봅 니 다.
public void bind(ServletRequest request) {
	MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
	MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
	if (multipartRequest != null) {
		bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
	}
	/**
	 *          ,              MutablePropertyValues,         
	 */
	addBindValues(mpvs, request);
	doBind(mpvs);
}

protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
}

2. ServletRequestDataBinder 사용자 정의
public class CustomServletRequestDataBinder extends ServletRequestDataBinder {

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

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

    @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);
    }

    private String underLineToCamel(final String value) {
        final StringBuffer sb = new StringBuffer();
        Matcher m = this.underLinePattern.matcher(value);
        while (m.find()){
            m.appendReplacement(sb, m.group(1).toUpperCase());
        }
        m.appendTail(sb);
        return sb.toString();
    }

}

3. 사용
@SpringBootApplication
public class MainApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CustomServletModelAttributeMethodProcessor(true));
    }
    
}

좋은 웹페이지 즐겨찾기