我有以下Post課程:
@Entity
@Table(name = "posts")
@Getter
@Setter
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id",
scope = Long.class)
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String subtitle;
private String content;
private String img_url;
@CreationTimestamp
private Timestamp created_on;
@UpdateTimestamp
private Timestamp last_updated_on;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner_id", nullable=false)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private User creator;
}
以及以下擴展 JpaRepository 的存盤庫
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
Optional<Post> findById(Long id);
List<Post> findAll();
}
當回傳findAll()以下控制器內部的結果時,只有第一個創建者專案被完整發送,其余的只包含 id:
@GetMapping("/news")
public List<Post> getNews() {
return postRepository.findAll();
}
這是我得到的 JSON 結果:
[
{"id":15,"title":"Title example #1","subtitle":"Subtitle example #1","content":"Lorem #1 ipsum dolor sit amet","img_url":null,"created_on":"2021-12-01T00:00:00.000 00:00","last_updated_on":"2021-12-01T00:00:00.000 00:00","creator":{"id":1,"username":"user-example","email":"[email protected]","roles":[{"id":1,"name":"ROLE_USER"}]}}
,{"id":25,"title":"Title example #2","subtitle":"Subtitle example #2","content":"Lorem #2 ipsum dolor sit amet","img_url":null,"created_on":"2021-12-01T00:00:00.000 00:00","last_updated_on":"2021-12-01T00:00:00.000 00:00","creator":1}
]
為什么會這樣?有沒有辦法為 JSON 陣列中的每個元素獲取整個子物件?
謝謝
編輯:添加了User類
@Entity
@Table( name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
})
@DiscriminatorValue(value="USER")
public class User extends OwnerEntity {
@NotBlank
@NotNull
@Size(max = 20)
private String username;
@NotBlank
@NotNull
@Size(max = 50)
@Email
private String email;
@NotBlank
@Size(max = 120)
@JsonIgnore
private String password;
@CreationTimestamp
private Timestamp created_on;
@UpdateTimestamp
private Timestamp last_updated_on;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable( name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY)
private Set<Institution> institutions;
@OneToMany(mappedBy="creator", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
protected Set<Post> posts;
@ManyToMany(fetch = FetchType.LAZY)
private Set<Institution> following;
}
編輯 2:添加了 OwnerEntity 類
@Entity
@Table(name = "entities")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn
@Getter
@Setter
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id",
scope = Long.class)
public class OwnerEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
}
uj5u.com熱心網友回復:
你的OwnerEntity也有@JsonIdentityInfo。在其參考檔案中,我們可以閱讀以下內容:
用于指示帶注釋的型別或屬性的值應該被序列化的注釋,以便實體包含額外的物件識別符號(除了實際的物件屬性),或者作為由參考完整序列化的物件 id 組成的參考。在實踐中,這是通過將第一個實體序列化為完整物件和物件標識,并將對物件的其他參考序列化為參考值來完成的。
這完美地解釋了為什么你會得到這樣的 JSON。如果您不想要這個,只需洗掉@JsonIdentityInfo它,但它可能會在序列化雙向關系時修復無限遞回(您可以在以下在線資源中閱讀更多相關資訊https://www.baeldung.com/jackson-bidirectional-關系和無限遞回)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/371476.html
