问题描述
我正在为购物清单开发一个Spring Boot应用程序。
为此,我使用Spring Data Rest通过REST API导出我的实体。
I'm developing a Spring Boot Application for a shopping list.For this I use Spring Data Rest to export my entities through a REST API.
My Architecture看起来像这样
My Architecture looks like this
我有 ShoppingItem :
public class ShoppingItem {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name = "articleId", nullable = false)
private Article article;
private Integer number;
private boolean bought;
public ShoppingItem(){
this.article = null;
this.number = 0;
this.bought = false;
}
}
此购物项目包含一篇文章是一个导出的资源。
This shopping item contains an Article which is an exported Resource.
文章如下所示:
public class Article {
@Id
@GeneratedValue
private Long id;
@Column(unique = true)
private String name;
private Integer price;
}
当我请求ShoppingItem时,答案如下:
When i request a ShoppingItem the answer looks like this:
{
id: 94,
number: 1,
bought: false,
_links: {
self: {
href: "https://myDomain.tld/api/shoppingItems/94"
},
article: {
href: "https://myDomain.tld/api/shoppingItems/94/article"
}
}
}
请求 ShoppingItem
时,是否可以在_embedded中包含 Article
所以回复看起来像这样吗?
Is it possible to include the Article
in _embedded when requesting the ShoppingItem
so the response looks like this?
{
id: 94,
number: 1,
bought: false,
_links: {
self: {
href: "https://myDomain.tld/api/shoppingItems/94"
},
article: {
href: "https://myDomain.tld/api/shoppingItems/94/article"
}
},
_embedded: {
article: {
id: '999',
name: 'someThing',
price: '1.99'
}
}
}
更新1
使用时接受:application / x-spring-data-verbose + json
回复如下:
{
id: 94
number: 1
bought: false
links: [2]
0: {
rel: "self"
href: "https://wg.yannic-klem.de/api/shoppingItems/94"
}-
1: {
rel: "article"
href: "https://wg.yannic-klem.de/api/shoppingItems/94/article"
}-
-
content: [0]
}
内容列表始终为空:(
更新2:
有关我的架构的更多信息,请随时查看我的Github回购:
For more information about my architecture feel free to have a look at my Github repo : https://github.com/Yannic92/ShoppingList/tree/master/src/main/java/de/klem/shopping
推荐答案
尝试添加此接受发出请求时
标题:
Accept: application/x-spring-data-verbose+json
另外,看看详细解释了这一点。
Also, take a look at this post where this is explained in details.
这篇关于Spring Data Rest - 在_embedded中包含嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!