好吧,您可以为 PropertyNamingStrategy 提供一个如下所示的实现:
import org.codehaus.jackson.map.MapperConfig;
import org.codehaus.jackson.map.PropertyNamingStrategy;
import org.codehaus.jackson.map.introspect.AnnotatedField;
import org.codehaus.jackson.map.introspect.AnnotatedMethod;
public class CustomNameStrategy extends PropertyNamingStrategy {
@Override
public String nameForField(MapperConfig config, AnnotatedField field, String defaultName) {
return convert(defaultName);
}
@Override
public String nameForGetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName) {
return convert(defaultName);
}
@Override
public String nameForSetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName) {
return convert(defaultName);
}
public String convert(String defaultName) {
char[] arr = defaultName.toCharArray();
StringBuilder nameToReturn = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '_') {
nameToReturn.append(Character.toUpperCase(arr[i + 1]));
i++;
} else {
nameToReturn.append(arr[i]);
}
}
return nameToReturn.toString();
}
}
然后在您的实现或执行反化化的类中,您可以拥有:
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new CustomNameStrategy());
YourClass yourClass = mapper.readValue(yourJsonString, YourClass.class);