Spring Boot-JSON使用自定义对象数组序列化自定义对象
有这个课程:
@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
关联的数组.标签看起来像这样:
which has an array of Tag
associated with it. Tag looks like this:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private long activityId;
private String keyword;
public Tag(String keyword){
this.keyword = keyword;
activityId = -1;
}
Activity
和Tag
都是实体,并且都具有存储库:
Both Activity
and Tag
are Entitys and also both have Repositorys:
public interface TagRepository extends CrudRepository<Tag, Long> {
}
public interface ActivityRepository extends CrudRepository<Activity, Long> {
}
有一个create-方法可以在数据库中存储Activity
对象:
There is this create - method which stores an Activity
Object in the database:
@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:
I transmit this JSON via POST:
{
"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,并且都具有默认的ctor,我只是没有在此处显示它们以使问题尽可能简短.
Both classes have getter/setter for each member variable and also both have default ctors, I just didn't show them here to keep the question as short as possible.
首先,您需要在活动和标签之间创建关系.活动有很多标签,那么关系应该是oneToMany.
First of all you need to create a relationship between activity and tags. Activity has many tags, then the relation should be 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 ,这意味着您将手动处理标签实体的保存.
first of all you are not using the property CASCADE.ALL which means that you will handle the save of your tags entity manually.
@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);
}