我剛剛開始使用 Spring boot,并且正在使用默認存盤庫 api 將 db 資料檢索為 json。我添加了@ManyToOne與我的歌曲和藝術家物體的關系。
但是現在我沒有從服務器的 json 回應中獲取 Artist 物件,而且我并不清楚如何在不錯過 PagingAndSorting 存盤庫中的分頁功能的情況下包含它。
我正在使用 spring-data-rest-jpa。
我的回復現在看起來像:
"_embedded": {
"songs": [
{
"id": 1,
"title": "SongTitle",
"genre": "Rap",
"length": 500,
"_links": {
"self": {
"href": "http://localhost:8080/api/songs/1"
},
"song": {
"href": "http://localhost:8080/api/songs/1"
},
"artist": {
"href": "http://localhost:8080/api/songs/1/artist"
}
}
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/api/songs?page=0&size=1"
},
"self": {
"href": "http://localhost:8080/api/songs?size=1"
},
"next": {
"href": "http://localhost:8080/api/songs?page=1&size=1"
},
"last": {
"href": "http://localhost:8080/api/songs?page=19&size=1"
},
"profile": {
"href": "http://localhost:8080/api/profile/songs"
}
},
"page": {
"size": 1,
"totalElements": 20,
"totalPages": 20,
"number": 0
}
}
但我希望它是這樣的:
"_embedded": {
"songs": [
{
"id": 1,
"title": "SongTitle",
"genre": "Rap",
"length": 500,
"artist": {
"id": 1,
"name": "Artistname"
}
"_links": {
"self": {
"href": "http://localhost:8080/api/songs/1"
},
"song": {
"href": "http://localhost:8080/api/songs/1"
},
"artist": {
"href": "http://localhost:8080/api/songs/1/artist"
}
}
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/api/songs?page=0&size=1"
},
"self": {
"href": "http://localhost:8080/api/songs?size=1"
},
"next": {
"href": "http://localhost:8080/api/songs?page=1&size=1"
},
"last": {
"href": "http://localhost:8080/api/songs?page=19&size=1"
},
"profile": {
"href": "http://localhost:8080/api/profile/songs"
}
},
"page": {
"size": 1,
"totalElements": 20,
"totalPages": 20,
"number": 0
}
}
歌曲.java
@Getter
@Setter
@Entity
@Table(name = "song")
public class Song {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
private Long id;
@NotNull
@NotBlank(message = "The song has to have a title")
private String title;
@NotNull
@NotBlank(message = "The song has to have a genre")
private String genre;
@NotNull
@Min(value = 1, message = "The song has to have a song length in seconds")
private int length;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "artist_id", referencedColumnName = "id")
private Artist artist;
/* @Version
private long version;*/
public Song() {
}
public Song(String title, Artist artist, String genre, int length) {
this.title = title;
this.artist = artist;
this.genre = genre;
this.length = length;
}
public void setArtist(Artist artist) {
this.artist = artist;
}
public Artist getArtist() {
return artist;
}
}
藝術家.java
@Getter
@Setter
@Entity
@Table(name = "artist")
public class Artist {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@NotNull
@NotBlank(message = "The artist has to have a name")
private String name;
@JsonIgnore
@OneToMany(mappedBy = "artist")
private List<Song> songs;
public Artist() {
}
public Artist(String name) {
this.name = name;
}
為了測驗,我在 SongController 中撰寫了一個方法:
@GetMapping
List<Song> getSongs() {
return songRepository.findAll();
}
結果包括 Artist 物件,但不會對其進行任何分頁。我怎么能包括它?
json結果:
[
{
"id": 1,
"title": "SongTitle",
"genre": "Rap",
"length": 500,
"artist": {
"id": 1,
"name": "ArtistName"
}
}
]
uj5u.com熱心網友回復:
在您提出所有有用的建議后,我找到了答案:我已將控制器中方法的回傳型別更改為 Page 并使用了如下所示的 PageRequest 類:
@GetMapping
public Page<Song> getSongs(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "5") int size) {
PageRequest pr = PageRequest.of(page, size);
return songRepository.findAll(pr);
}
還使用了一些默認值來避免一些例外;)
uj5u.com熱心網友回復:
使用@JsonIdentityInfo 或@JsonIgnore 并洗掉@JsonBackReference。這是@JsonIgnore 的示例
public class Artist {
public Long id;
public String name;
public List<Song> songs;
}
public class Song {
public Long id;
public String title;
public String genre;
public int length;
@JsonIgnore
public Artist artist;
}
@JsonManagedReference 或 @JsonBackReference 在這種情況下無濟于事(在https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion上閱讀更多資訊):
(...) 我們可以使用 @JsonIgnore 注釋來簡單地忽略關系的一側,從而破壞鏈。[我的補充:回圈呼叫鏈]
這是@JsonIdentityInfo 的示例:
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Artist { ... }
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Song { ... }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521163.html
