【发布时间】:2022-10-24 03:34:11
【问题描述】:
我刚刚开始使用 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"
}
}
]
【问题讨论】:
-
spring data rest 默认使用 HATEOAS 返回只是子对象的链接。因此,您的第一个任务需要您自己实现 HATEOS 表示。第二个任务:您只需返回一个对象列表。那么为什么要有分页细节呢?
-
我知道这些链接,但我不想用它们来提出额外的请求,但没关系。我知道我返回的只是一个列表,这就是我问的原因,我怎么能包括分页。毕竟我已经解决了它,我会发布一个答案。
标签: java spring hibernate jpa spring-data-jpa