【问题标题】:Using Nested Projections in Spring Data REST在 Spring Data REST 中使用嵌套投影
【发布时间】:2015-02-05 21:59:57
【问题描述】:

如何在 OrderProjection 使用 ItemProjection 使用 Spring Data REST 呈现项目的情况下获得预期输出

GET /orders/1?projection=with_items

预测:

@Projection(name = "summary", types = Item.class)
public interface ItemProjection {
    String getName();
}

@Projection(name = "with_item", types = Order.class)
public interface OrderProjection {
    LocalDateTime getOrderedDate();
    Status getStatus();
    Set<ItemProjection> getItems(); // this is marshalling as Set<Item> (full Item graph)
}

目前正在输出:

{
  "status" : "PAYMENT_EXPECTED",
  "orderedDate" : "2014-11-09T11:33:02.823",
  "items" : [ {
    "name" : "Java Chip",
    "quantity" : 1,
    "milk" : "SEMI",
    "size" : "LARGE",
    "price" : {
      "currency" : "EUR",
      "value" : 4.20
    }
  } ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/orders/1{?projection}",
      "templated" : true
    },
    "restbucks:items" : {
      "href" : "http://localhost:8080/orders/1/items"
    },
    "curies" : [ {
      "href" : "http://localhost:8080/alps/{rel}",
      "name" : "restbucks",
      "templated" : true
    } ]
  }
}

预期输出:

{
  "status" : "PAYMENT_EXPECTED",
  "orderedDate" : "2014-11-09T11:33:02.823",
  "items" : [ {
    "name" : "Java Chip"
  } ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/orders/1{?projection}",
      "templated" : true
    },
    "restbucks:items" : {
      "href" : "http://localhost:8080/orders/1/items"
    },
    "curies" : [ {
      "href" : "http://localhost:8080/alps/{rel}",
      "name" : "restbucks",
      "templated" : true
    } ]
  }
}

【问题讨论】:

  • 哇!不知道我可以在投影中指定投影接口!它在文档中的某处提到过吗?

标签: java spring spring-data-rest


【解决方案1】:

您遇到了DATAREST-394,该问题已在几天后修复,并将进入 2.2.2 和 2.3 RC1。它已在所述版本的快照中提供,请随意试用。

【讨论】:

  • 感谢 Oliver,我为 spring-data-rest-webmvc 使用了 2.3.0.BUILD-SNAPSHOT,这解决了 REST 问题,但也为 spring-framework-bom 使用了 4.1.2.RELEASE,我是发现我所有与 Spring MVC 中的 @RequestMapping 关联的旧 URL 都给出 404 错误
  • well 404 由于 MVC 上下文中缺少组件扫描而发生。
  • 这个问题在 Spring Data Rest 的 2.3.0.M1 中再次出现,我希望那只是因为是里程碑版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2018-04-18
  • 2016-01-20
  • 2020-01-10
  • 2022-12-02
  • 1970-01-01
相关资源
最近更新 更多