aix
2024-08-06 7f1e5a861d9944cc28285cd36ee47b33dee04446
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.dji.sample.configuration.mvc;
 
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.web.servlet.mvc.method.annotation.ExtendedServletRequestDataBinder;
 
import javax.servlet.ServletRequest;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author sean
 * @version 1.2
 * @date 2022/9/9
 */
public class GetSnakeDataBinder extends ExtendedServletRequestDataBinder {
 
    public GetSnakeDataBinder(Object target, String objectName) {
        super(target, objectName);
    }
 
    @Override
    protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
        List<PropertyValue> propertyValueList = mpvs.getPropertyValueList();
        List<PropertyValue> values = new ArrayList<>(propertyValueList);
        for (PropertyValue property : values) {
            String name = convertSnake(property.getName());
            if (!property.getName().equals(name)) {
                mpvs.addPropertyValue(new PropertyValue(name, property.getValue()));
                propertyValueList.remove(property);
            }
        }
        super.addBindValues(mpvs, request);
    }
 
    private String convertSnake(String key) {
        StringBuilder sb = new StringBuilder();
        boolean isChange = false;
        for (char c : key.toCharArray()) {
            if (c == '_') {
                isChange = true;
                continue;
            }
            if (isChange) {
                sb.append((char) (c - 32));
                isChange = false;
                continue;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}