简体   繁体   中英

Using Nested Projections in Spring Data REST

How to get expected output below where OrderProjection uses ItemProjection to render Items using Spring Data REST

GET /orders/1?projection=with_items

Projections :

@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)
}

Currently getting as output:

{
  "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
    } ]
  }
}

Expected Output:

{
  "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
    } ]
  }
}

You're running into DATAREST-394 which has been fixed a few days a go and will be making it into 2.2.2 and 2.3 RC1. It's already available in the snapshots for said versions, feel free to give them a spin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM