Spring Boot REST Resource not showing linked objects (sets)
Asked Answered
S

1

1

I've a database and some classes. These classes are linked with OneToMany, and so on.

If I print the object itself with spring it contains everything. But if I print it with the Resource feature, it contains only the variables, which are no collections or linked otherwise with an other class.

How can I add the collections to the output?

Songful answered 15/10, 2015 at 18:29 Comment(2)
Are you using Spring Data REST? Look into projections if so.Teillo
I'm using Spring Data REST. What do you mean with "projections"?Songful
T
1

By default Spring Data REST does not show associated resources except as links. If you want that you have to define projections that describe the fields you want to see, whether they're simple fields like the ones you describe or associated resources. See

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts

For example say you have a Service resource with associations to resources like serviceType, serviceGroup, owner, serviceInstances and docLinks. If you want those to show up in the response body you can create a projection:

package my.app.entity.projection;

import org.springframework.data.rest.core.config.Projection;
...

@Projection(name = "serviceDetails", types = Service.class)
public interface ServiceDetails {   
    String getKey();
    String getName();   
    ServiceType getType();
    ServiceGroup getGroup();
    Person getOwner();
    List<ServiceInstance> getServiceInstances();    
    List<DocLink> getDocLinks();
    String getPlatform();
}

Then GET your URL with the projection:

http://localhost:8080/api/services/15?projection=serviceDetails

The result will include the projected properties:

{
  "name" : "MegaphoneService",
  "key" : "megaphone",
  "type" : {
    "key" : "application",
    "name" : "User Application",
    "description" : "A service that allows users to use a megaphone."
  },
  "owner" : null,
  "serviceInstances" : [ {
    "key" : "megaphone-a-dr",
    "description" : null,
    "loadBalanced" : true,
    "minCapacityDeploy" : null,
    "minCapacityOps" : 50
  }, ... ],
  ...
}
Teillo answered 15/10, 2015 at 18:41 Comment(4)
is this really the only solution? I mean I've classes for all the database entries and now I've to make interfaces too?Songful
Only in cases where you actually need the data inlined. It's different than e.g. JPA/Hibernate where you might have lazy loading, because with a HTTP response you're not going to have a proxy there to lazy load. If SDR were to default to showing all the associated resources that would lead to large responses (with the corresponding joins/payloads) in many contexts where links are sufficient. So SDR returns everything it can without joining but offers the option to join as necessary.Teillo
I'm using Pageable in other cases and there it shows me the content by default. Any way to do it with that?Songful
Sometimes I get all the sub informations and sometimes I don't... || Is there a way to hide something from specific calls? (for example the 'root' element? - I'm always getting all accounts with passwords...)Songful

© 2022 - 2024 — McMap. All rights reserved.