刚刚测试,这有效:
public class Coordinates { byte red; @JsonProperty("r") public byte getR() { return red; } @JsonProperty("red") public void setRed(byte red) { this.red = red; }}这个想法是方法名称应该不同,所以杰克逊将其解析为不同的字段,而不是一个字段。
这是测试代码:
Coordinates c = new Coordinates();c.setRed((byte) 5);ObjectMapper mapper = new ObjectMapper();System.out.println("Serialization: " + mapper.writevalueAsString(c));Coordinates r = mapper.readValue("{"red":25}",Coordinates.class);System.out.println("Deserialization: " + r.getR());结果:
Serialization: {"r":5}Deserialization: 25


