5

I am using spring-boot-starter-parent 1.4.1.RELEASE.

  1. Spring boot @ResponseBody doesn't serialize entity id by default.

If I use exposeIdsFor returns the id but I need to configure this for each class

e.g

RepositoryRestConfiguration.exposeIdsFor(User.class); RepositoryRestConfiguration.exposeIdsFor(Address.class);

Is there a simpler configuration to do this without configuring each entity class.

Refer: https://jira.spring.io/browse/DATAREST-366

  1. The REST resource will render the attribute as a URI to it’s corresponding associated resource. We need to return the associated object instead of URI.

If I use Projection, it will returns the associated objects but I need to configure this for each class

e.g

@Entity
public class Person {

  @Id @GeneratedValue
  private Long id;
  private String firstName, lastName;

  @ManyToOne
  private Address address;
  …
}

PersonRepository:

interface PersonRepository extends CrudRepository<Person, Long> {}

PersonRepository returns,

{
  "firstName" : "Frodo",
  "lastName" : "Baggins",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons/1"
    },
    "address" : {
      "href" : "http://localhost:8080/persons/1/address"
    }
  }
}

My Projection:

@Projection(name = "inlineAddress", types = { Person.class }) 
interface InlineAddress {

  String getFirstName();

  String getLastName();

  Address getAddress(); 
}

After adding projection, it returns..

{
  "firstName" : "Frodo",
  "lastName" : "Baggins",
  "address" : { 
    "street": "Bag End",
    "state": "The Shire",
    "country": "Middle Earth"
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons/1"
    },
    "address" : { 
      "href" : "http://localhost:8080/persons/1/address"
    }
  }
}

If some other classes having the association as an address, then I need to add projection for those classes also.

But I don't want to configure for each classes. How to implement the dynamic Projection? So that all my entities will return the nested object in response.

Refer: https://jira.spring.io/browse/DATAREST-221

SST
  • 2,054
  • 5
  • 35
  • 65

1 Answers1

1

Regarding your first question, which was already answered here, exposeIdsFor accepts an arbitrary number of arguments. Also, as mentionned in this thread If all your entity classes are located in the same package, you could get a list of your classes using the Reflections library and feed it to exposeIdsFor().

Community
  • 1
  • 1
Marc Tarin
  • 3,109
  • 17
  • 49
  • Ok. I already tried with this.. it is working fine... Is this a only way to return id in the response for all the entities?? – SST Oct 24 '16 at 04:51
  • We can't exposeIds as the class responsible for configuration doesn't know about dependencies packages we plug into the application. Is there another way to include id for nested object ? – Dimitri Kopriwa Feb 02 '19 at 15:13