繁体   English   中英

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

[英]Spring Boot - JSON Serialize Custom Object with Array of Custom Objects

有这个课程:

@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> {

}

有一个create方法将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方法,并且都具有默认的ctor,我只是没有在此处显示它们以使问题尽可能简短。

首先,您需要在活动和标签之间创建一个关系。 活动有很多标签,那么关系应该是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);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM