Empty references and collections in Projections processing issue

93 views Asked by At

there is a problem during creation of Projection class for one of my entities. the issue raises when one of the object's embedded elements is empty.

the projection class source is:

@Projection(name = "summary", types = {Question.class})
public interface QuestionProjection {

    Long getId();

    @Value("#{target.getResource().getResourceUrl()}")
    String getResourceUrl();
}

the JSON object's structure is here (corresponding @Entity class with getter/setter methods is created accordingly):

"_embedded" : {
"Rest API" : [ {
  "text" : "text",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/rest/questions/9{?projection}",
      "templated" : true
    },
    "resource" : {
      "href" : "http://localhost:8080/rest/questions/9/resource"
    },
    "approver" : {
      "href" : "http://localhost:8080/rest/questions/9/approver"
    }
  }

i see a blank page (no data at all, the element is empty) when i'm following the link http://localhost:8080/rest/questions/9/resource.

I'm also getting the error presented below when i use the newly created projection:

Resolving exception from handler [public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException

so the question is - how can I get rid of NPE in case of empty embedded element (collection)? can I have some kind of conditional processing of the embedded elements?

thank you in advance.

0

There are 0 answers