在Spring Data REST中使用嵌套投影

9

如何获得预期输出,其中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
    } ]
  }
}

4
哇!我不知道我可以在投影中指定投影接口!文档中有提到吗? - o_nix
1个回答

5

您正在遇到已经修复了几天的 DATAREST-394 问题,它将出现在2.2.2和2.3 RC1版本中。它已经可以在这些版本的快照中使用,请随意尝试。


谢谢Oliver,我使用了2.3.0.BUILD-SNAPSHOT的spring-data-rest-webmvc,这解决了REST问题,但是同时使用4.1.2.RELEASE的spring-framework-bom后,我发现所有与Spring MVC中@RequestMapping相关的旧URL都会出现404错误。 - fortm
由于MVC上下文中缺少组件扫描,导致404错误发生。在2.2.1 Spring data rest版本之前,可以通过应用程序上下文中的组件扫描来解决此问题。 - fortm
1
这个问题在Spring Data Rest的2.3.0.M1版本中重新出现了,希望这只是里程碑版本的原因。 - fortm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接