Creating JSON by combining fields from two classes

ASharma7 :

I have two classes : class A , class B

class A{
 private int F1;
 private String F2;
}

class B{
 private int F3;
 private String F4;
 private String F5;
}

I want a JSON like this:

{
   "F1": 123
   "F2": "ABC"
   "F3": 456
   "F4": "CDE"
   "F5": "FGH"
}

I am using springboot which creates JSON as soon as I return object from @RestController. How can I achieve the above json using these two classes.

Note : 1.) I already know that by using class A extends B , I can achieve this but I am looking for some spring based method to achieve this

2.) Using @Embeddable in class B & then creating reference in Class A creates additional tag B in JSON as shown :

{
   "F1": 123
   "F2": "ABC"
    b: {
          "F3": 456
          "F4": "CDE"
          "F5": "FGH"
    }
}
Sanjay :

How about using jackson @JsonUnwrapped?

http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html

public class A{

    @JsonUnwrapped
    private B b;

    public User getB() ...
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=114511&siteId=1