您看到的问题是由于 Jackson 使用 Java Bean 命名约定来找出 Java 类中的 Json 属性。
以下是您看到的具体问题的参考,建议不要将您所在领域的前两个字母中的任何一个大写。如果您使用像IntelliJ或eclipse这样的IDE,并让IDE为您生成设置器,您会注意到发生了相同的“行为”,您最终会得到以下方法:
public void setaLogId(String aLogId) {
this.aLogId = aLogId;
}
public String getaLogId() {
return aLogId;
}
因此,当您将“L”更改为小写时,Jackson能够弄清楚您想要映射的字段。
话虽如此,您仍然可以使用“aLogId”字段名称并使Jackson工作,您所要做的就是在其中使用注释。@JsonPropertyaLogId
@JsonProperty("aLogId")
private String aLogId;
以下测试代码将演示这将如何工作:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
@JsonProperty("aLogId")
private String aLogId;
public void setaLogId(String aLogId) {
this.aLogId = aLogId;
}
public String getaLogId() {
return aLogId;
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
Test test = new Test();
test.setaLogId("anId");
try {
System.out.println("Serialization test: " + objectMapper.writeValueAsString(test));
String json = "{\"aLogId\":\"anotherId\"}";
Test anotherTest = objectMapper.readValue(json, Test.class);
System.out.println("Deserialization test: " +anotherTest.getaLogId());
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试的输出为:
Serialization test: {"aLogId":"anId"}
Deserialization test: anotherId