Is there an easy way to smooth the properties of a JSON object using Jackson?

Given an entity class with a composite key that is controlled by hibernation, is there an easy way to smooth out the key properties in the object itself?

Given:

{ "key": { "field1": 1, "field2": 2 }, "prop": "value" } 

I would prefer it to be serialized as:

 { "field1": 1, "field2": 2, "prop": "value" } 

I really do not want to implement JsonSerializableWithType , since it is fine at the moment, it is just a key class, I want the fields to be flattened.

+4
source share
1 answer

Jackson uses reflection, and you can manipulate getters / setters. You can add @JsonIgnore to getKey() and add two methods

 @JsonProperty("field1") private int getField1() @JsonProperty("field2") private int getField2() 

You might want to implement setField1() and setField2() .

+1
source

Source: https://habr.com/ru/post/1397562/


All Articles