Java – is there a simple way to flatten JSON object properties using Jackson?

Given an entity class with compound keys managed by hibernate, is there a simple way to flatten key properties to the object itself?

Whereas:

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

I want to serialize it as:

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

I really don't want to implement jsonserializablewithtype, because it's good now. It's just a key class. I want the fields to be flattened

Solution

Jackson uses reflection, you can manipulate getter / setter You can add @ jsonignore to getKey () and add two methods

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

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

You may also want to implement setfield1() and setfield2()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>