gpt4 book ai didi

java - Spring Boot - 使用自定义对象数组 JSON 序列化自定义对象

转载 作者:行者123 更新时间:2023-12-02 11:55:54 25 4
gpt4 key购买 nike

有这样一个类:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private int key;
private String text;
private Tag[] tags;
private String title;
private boolean valid;
public Activity(int key, String text, Tag[] tags, String title) {
this.key = key;
this.text = text;
this.tags = tags;
this.title = title;
valid = true;
}

它有一个与之关联的Tag数组。标签如下所示:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private long activityId;
private String keyword;

public Tag(String keyword){
this.keyword = keyword;
activityId = -1;
}

ActivityTag 都是实体,并且都有存储库:

public interface TagRepository extends CrudRepository<Tag, Long> {

}

public interface ActivityRepository extends CrudRepository<Activity, Long> {

}

有一个创建方法,它在数据库中存储 Activity 对象:

  @PostMapping
public Activity create(@RequestBody Activity input) {
if(input.getTags() != null) {
for(Tag t : input.getTags()) {
t.setActivityId(input.getId()); //associate which activity has which tags
tagRepository.save(t);
}
}

return activityRepository.save(input);
}

我通过 POST 传输此 JSON:

{
"key":2,
"text":"Hello",
"tags":[{
"keyword":"h"
},{
"keyword":"b"
}
],
"title":"world"
}

但我收到此错误:

"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.orm.jpa.JpaSystemException",
"message": "could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize"

这两个类都有每个成员变量的 getter/setter,并且都有默认构造函数,我只是没有在此处显示它们,以使问题尽可能简短。

最佳答案

首先,您需要在 Activity 和标签之间创建关系。 Activity 有很多标签,那么关系应该是 oneToMany。

http://www.baeldung.com/hibernate-one-to-many

@Entity
@Table(name="ACTIVITY")
public class Activity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private int key;
private String text;

@OneToMany(mappedBy="activity")
private List<Tag> tags;

private String title;
private boolean valid;
public Activity(int key, String text, List<Tag> tags, String title) {
this.key = key;
this.text = text;
this.tags = tags;
this.title = title;
valid = true;
}
}

@Entity
@Table(name="TAG")
public class Tag {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

@ManyToOne
@JoinColumn(name="activity_id", nullable=false)
private Activity activity;

private String keyword;

public Tag(String keyword){
this.keyword = keyword;
activityId = -1;
}
}

首先,您没有使用属性CASCADE.ALL,这意味着您将手动处理标签实体的保存。

@PostMapping
public Activity create(@RequestBody Activity input) {
if(input.getTags() != null) {
//you are saving all the nested tags manually before saving the activity
for(Tag t : input.getTags()) {
tagRepository.save(t);
}
}
//now you are saving activity which contains these tags
return activityRepository.save(input);
}

关于java - Spring Boot - 使用自定义对象数组 JSON 序列化自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606934/

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