您看到的问题是由于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运行所有您需要做的就是使用其中的
@JsonProperty注释
aLogId。
@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



