gpt4 book ai didi

java - 如何在 Spring-boot REST 上使用 Jackson 更改响应结构

转载 作者:行者123 更新时间:2023-12-02 01:59:57 24 4
gpt4 key购买 nike

我正在使用 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",
}
},
]
}

最佳答案

您可以为您的实体创建自己的序列化器。

对我来说,最好的方法是使用实​​体所需的属性创建 DTO 类。

  • DAO @Repository 类管理实体。
  • 服务调用 DAO 方法并将实体转换为 DTO。
  • @Controllers 调用服务方法并以 JSON 形式返回 DTO。

关于java - 如何在 Spring-boot REST 上使用 Jackson 更改响应结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51746353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com