代码之家  ›  专栏  ›  技术社区  ›  K. Ayoub

Spring boot使用jackson更改json响应结构

  •  0
  • K. Ayoub  · 技术社区  · 6 年前

    我使用的是springbootsrest,我想在序列化期间更改JSON响应的结构。
    我有以下型号:
    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;
        }
    }  
    

    titleElebrityType.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 回复  |  直到 6 年前
        1
  •  2
  •   tsarenkotxt    6 年前

    你可以用 DTO公司 :

    将数据封装在可以通过 网络:数据传输对象。 细节 DTO

    也可以使用推土机:

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

    如何使用 推土机 具有 Spring Boot
    推土机 注释映射 documentation