1

I am using the inlineAddress sample of the Spring Data Rest documentation.

/persons return the address inline as expected.

Now I add a projection to the AddressRepository

@RepositoryRestResource(excerptProjection = AddressProjection.class)

Which is as below

@Projection(name = "AddressesProjection", types = Address.class)
public interface AddressProjection {

public String getStreet();
}

This is causing the /persons call to have an address projection as _embedded

    {
      "_embedded" : {
        "persons" : [ {
           "firstName" : "dfdf",
           "lastName" : "2",
           "addresses" : [ {
            "street" : "tx",
         "state" : "tx",
        "country" : "dfd"
      } ],
      "_embedded" : {
        "addresses" : [ {
          "street" : "tx",
          "_links" : {
            "self" : {
              "href" : "/api/addresses/1{?projection}",
              "templated" : true
            }
          }
        } ]
      },
      "_links" : {
        "self" : {
          "href" : " api/persons/1{?projection}",
          "templated" : true
        },
        "addresses" : {
          "href" : " /api/persons/1/addresses"
        }
        }
       } ]
     }
   }

I dont know if this is expected. This behaviour is causing repeated information when I have a oneToMany relation like order/Comments and have projection on both order and comments and when I access order/1/comments I see the order also embedded for each comments.

3
  • Could you post your versions? (SDR, SF, etc) Commented Sep 11, 2015 at 0:23
  • Spring Boot v1.2.5.RELEASE, Spring v4.1.7.RELEASE Commented Sep 11, 2015 at 0:41
  • Was this ever resolved? I am running into the same problem, especially with the oneToMany problem of repeating Commented Nov 16, 2016 at 17:11

2 Answers 2

2

I have a similar issue with spring-data-rest 2.5.6. So I'd like to add this.

If :

  • an A entity has a @OneToMany relationship to a B entity
  • the B entity's repository @RepositoryRestResource contains an excerptProjection

Then spring-data-rest will embed the B entity's list in any A entity (in _embedded).

If there is no excerptProjection, the list won't be embedded.

I'd like to be able to choose what I want to be embedded, but at moment, I found no solution to do so.

Sign up to request clarification or add additional context in comments.

Comments

0

For anybody that is looking for the answer to this issue, I actually did find a solution.

Based on the example in Spring-RestBucks you will need to have a custom RepresentationModelProcessor on A entity.

Also consider the official Spring HATEOAS Documentation on RepresentationModelProcessor.

Applying to the above example, you would do:

public class PersonRepresentationProcessor implements RepresentationModelProcessor<EntityModel<Person>> {

  private final EntityLinks entityLinks;

 @Override
 public EntityModel<Person> process(EntityModel<Person> personModel) {
  // create new EntityModel without the embedded collection

  TypedEntityLinks<Person> typedPersonLink = entityLinks.forType(Person::getId);

  EntityModel<Person> newPerson = new EntityModel<>(personModel.getContent(),
                                                    personModel.getLinks());

  // add more links or other modifications
  return newPerson;
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.