Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    } ]
  }
}
like image 575
fortm Avatar asked Dec 08 '14 09:12

fortm


1 Answers

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.

like image 100
Oliver Drotbohm Avatar answered Oct 01 '22 02:10

Oliver Drotbohm