有这样一个类:
@Setter
@Getter
@JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Student {
private String bName;
}
序列化后,希望首字母大写,如下面的测试代码:
@Test
public void contextLoads() throws IOException {
Student test = new Student();
test.setBName("234234");
String s = objectMapper.writeValueAsString(test);
Assert.assertEquals("{\"BName\":\"234234\"}", s);
}
可实际运行后,结果与希望不一样:
org.junit.ComparisonFailure:
Expected :{"BName":"234234"}
Actual :{"Bname":"234234"}
jackson在序列化时把第二个大写字母n转成了小写,这是为什么呢?
以下是跟踪源码的过程:
直接找到:com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector#collectAll这个方法:
执行完_addFields(props)方法后:
执行完_addMethods(props)方法后:
一个是bName,一个是bname;
第一个bName取的是字段的名称,
第二个bname是取的它的set方法:
public static String okNameForIsGet