简体   繁体   中英

Spring-boot change the json response structure with jackson

I'm using spring boot REST, and I want to change the structure of the JSON response during serialize.
I have the following models:
Title.java

@Entity
public class Title {
    private Short id;
    private String name;
    private Collection<TitleCelebrity> titleCelebrities;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public Short getId() {
        return id;
    }

    public void setId(Short id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "title")
    public Collection<TitleCelebrity> getTitleCelebrities() {
        return titleCelebrities;
    }

    public void setTitleCelebrities(Collection<TitleCelebrity> titleCelebrities) {
        this.titleCelebrities = titleCelebrities;
    }
}

TitleCelebrity.java

@Entity
@Table(name = "title_celebrity")
public class TitleCelebrity {
    private TitleCelebrityPK id;
    private String characterName;
    private Title title;
    private TitleCelebrityType titleCelebrityType;

    @EmbeddedId
    @JsonIgnore
    public TitleCelebrityPK getId() {
        return id;
    }

    public void setId(TitleCelebrityPK id) {
        this.id = id;
    }

    @Basic
    @Column(name = "character_name")
    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    @MapsId("titleByTitleId")
    @ManyToOne
    @JoinColumn(name = "title_id", referencedColumnName = "id", nullable = false)
    @JsonIgnore
    public Title getTitle() {
        return title;
    }

    public void setTitle(Title title) {
        this.title = title;
    }

    @MapsId("titleCelebrityTypeByTitleCelebrityTypeId")
    @ManyToOne
    @JoinColumn(name = "title_celebrity_type_id", referencedColumnName = "id", nullable = false)
    public TitleCelebrityType getTitleCelebrityType() {
        return titleCelebrityType;
    }

    public void setTitleCelebrityType(TitleCelebrityType titleCelebrityType) {
        this.titleCelebrityType = titleCelebrityType;
    }
}  

TitleCelebrityType.java

@Entity
@Immutable
@Table(name = "title_celebrity_type")
public class TitleCelebrityType {
    private Short id;
    private String name;
    private Collection<TitleCelebrity> titleCelebrities;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public Short getId() {
        return id;
    }

    public void setId(Short id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "titleCelebrityType")
    @JsonIgnore
    public Collection<TitleCelebrity> getTitleCelebrities() {
        return titleCelebrities;
    }

    public void setTitleCelebrities(Collection<TitleCelebrity> titleCelebrities) {
        this.titleCelebrities = titleCelebrities;
    }
}

So when i make a request to get a Title here's how the JSON response look like:

{
    "id": 1,
    "name": "Vertigo",
    "titleCelebrities": [
            {
                "characterName": "John 'Scot",
                "titleCelebrityType": {
                    "id": 1,
                    "name": "Cast"
                }
            },
            {
                "characterName": "Madeleine ",
                "titleCelebrityType": {
                    "id": 1,
                    "name": "Cast"
                }
            },
            {
                "characterName": "a",
                "titleCelebrityType": {
                    "id": 2,
                    "name": "Director"
                }
            },
            {
                "characterName": "b",
                "titleCelebrityType": {
                    "id": 3,
                    "name": "Writer"
                }
            },
            {
                "characterName": "c",
                "titleCelebrityType": {
                    "id": 3,
                    "name": "Writer"
                }
            }
        ]
}

Is there anyway without changing the POJO classes (models) to make it look like this ?

{
    "id": 1,
    "name": "Vertigo",
    "titleCelebrities": [
        {
            "cast": {
                "characterName": "John 'Scot",
                "characterName": "Madeleine ",
            },
            "director": {
                "characterName": "a",
            },
            "writer": {
                "characterName": "b",
                "characterName": "c",
            }
        },
    ]
}

You can use DTO :

To encapsulate data in a value object that could be transferred over the network: a Data Transfer Object. In details DTO

And also you can use Dozer:

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. In details Dozer

How to use Dozer with Spring Boot
Dozer annotation mapping documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM