Java Spring数据Rest-在嵌入的_中包含嵌套资源

Java Spring数据Rest-在嵌入的_中包含嵌套资源,java,spring,rest,spring-data-rest,nested-resources,Java,Spring,Rest,Spring Data Rest,Nested Resources,我正在为购物清单开发一个Spring Boot应用程序。 为此,我使用SpringDataREST通过RESTAPI导出实体 我的架构是这样的 我有一个购物项目: public class ShoppingItem { @Id @GeneratedValue private Long id; @ManyToOne @JoinColumn(name = "articleId", nullable = false) private Article article; private Integer

我正在为购物清单开发一个Spring Boot应用程序。 为此,我使用SpringDataREST通过RESTAPI导出实体

我的架构是这样的

我有一个购物项目

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;
}

}
此购物项目包含作为导出资源的物品

文章如下所示:

public class Article {

@Id
@GeneratedValue
private Long id;

@Column(unique = true)
private String name;

private Integer price;
}
{
  id: 94,
  number: 1,
  bought: false,
  _links: {
    self: {
      href: "https://myDomain.tld/api/shoppingItems/94"
    },
    article: {
      href: "https://myDomain.tld/api/shoppingItems/94/article"
    }
  }
}
{
  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]
当我请求购买商品时,答案如下所示:

public class Article {

@Id
@GeneratedValue
private Long id;

@Column(unique = true)
private String name;

private Integer price;
}
{
  id: 94,
  number: 1,
  bought: false,
  _links: {
    self: {
      href: "https://myDomain.tld/api/shoppingItems/94"
    },
    article: {
      href: "https://myDomain.tld/api/shoppingItems/94/article"
    }
  }
}
{
  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]
请求
ShoppingItem
时,是否可以将
文章
包含在嵌入的_中,因此响应如下所示

{
  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 使用
Accept:application/x-spring-data-verbose+json时

响应如下所示:

public class Article {

@Id
@GeneratedValue
private Long id;

@Column(unique = true)
private String name;

private Integer price;
}
{
  id: 94,
  number: 1,
  bought: false,
  _links: {
    self: {
      href: "https://myDomain.tld/api/shoppingItems/94"
    },
    article: {
      href: "https://myDomain.tld/api/shoppingItems/94/article"
    }
  }
}
{
  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 repo:

尝试在发出请求时添加此
Accept
标题:

Accept: application/x-spring-data-verbose+json

另外,请查看详细解释的位置。

为文章定义一个摘录投影,并使用
节选投影

任何定义了摘录投影的资源都将添加到响应的
\u嵌入部分。这适用于关联的集合资源

在或Spring数据静止时读取更多信息

每当在_嵌入子句中使用目标类型的实例(您案例中的文章)时,都会使用摘录投影。因此,摘录是一种预览,在资源本身未呈现但指向的地方使用。。这通常是来自集合资源或关联的情况。
在Spring Data REST参考文档中阅读有关此主题的更多信息。

您好,感谢您的快速响应。我尝试了一个带有此accept标头的请求。显示了content属性,但其大小始终为0:(您所指的内容是文章?否您发布的链接告诉我,如果我使用您提到的标头,则会发生这种情况:(使用链接容器的链接和内容作为集合项的包装。).所以我的文章应该放在内容包装里,不是吗?我更新了我的问题,这样你就可以看到这个标题的输出。我忘记接受你的答案了。.标题对我不起作用,现在仍然不起作用,但是你链接的帖子中对奥利弗·吉尔克的预测的提示帮助了我。