Jackson, serialize one attribute of a reference

前端 未结 2 1925
一整个雨季
一整个雨季 2021-01-18 09:27

When serializing a Java object which has other object references, I need to serialize only one attribute of the nested object(usual case of foreign key, so serialize the \"i

2条回答
  •  -上瘾入骨i
    2021-01-18 09:59

    Just Found a way using Jackson 2.1+ .

    Annotate object references with(this will pick only the id attribute of AddressInformation):

    @JsonProperty(value = "defaultaddressid")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true) 
    public AddressInformation getDefaultAddress() {
        return defaultAddress;
    }
    

    Serialization works very well.

提交回复
热议问题