spring-boot用jackson改变json响应结构

Posted

技术标签:

【中文标题】spring-boot用jackson改变json响应结构【英文标题】:Spring-boot change the json response structure with jackson 【发布时间】:2019-01-13 23:22:15 【问题描述】:

我正在使用 Spring Boot REST,我想在序列化期间更改 JSON 响应的结构。 我有以下型号: 标题.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;
    

所以当我请求获取标题时,JSON 响应如下所示:


    "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"
                
            
        ]

有没有改变 POJO 类(模型)使其看起来像这样?


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

【问题讨论】:

【参考方案1】:

你可以使用DTO

将数据封装在可以传输的值对象中 网络:一个数据传输对象。 详情DTO

你也可以使用推土机:

Dozer 是一个 Java Bean 到 Java Bean 的映射器,它递归地将数据从一个对象复制到另一个对象。 详情Dozer

如何将Dozer与Spring Boot一起使用Dozer注解映射documentation

【讨论】:

以上是关于spring-boot用jackson改变json响应结构的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot:对日期(Date)类型数据的序列化

spring-boot:对日期(Date)类型数据的序列化

spring-boot 使用啥版本的 Jackson?

当jackson序列化json对象时如何防止hibernate5延迟获取?

Spring boot Jackson Json Serialization for List 实现类(PagedList)

如何在不影响原始 bean 的“客户”的情况下声明另一个 Jackson ObjectMapper?