3

How configure Spring Data REST to remove entity association links (left only "self") on Collection resource response of Repository interface endpoints, without set exported=false on @ResResource annotation (need keep exported the endpoints)

We has entities where the _links part has the bigest size on the response:

  • On Item resource _ links are useful to navigate throught the associations.

  • But on Collection Resources , mainly on large collections, this information is not important and makes the response unnecesary biger .

We need change this response:

    {
     "_embedded" : {
     "persons" : [ {
       "id" : "bat_3191",
       "name" : "B",
       "_links" : {     // 80% of response size !!
            "self" : {
                "href" : "http://localhost:8080/api/persons/bat_3191"
            },
            "orders" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/order"
            },
            "payments" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/payments"
            },
            "childrens" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/childrens"
            },
            "invoices" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/invoices"
            },
            "brands" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/brands"
            },
        }
      },
      { person [2] } 
        ... 
      { person [N] }
      ]
    },
      "_links" : {
       [page links]
    }

To a only "self" on _links part:

{
"_embedded" : {
"persons" : [ {
  "id" : "bat_3191",
  "name" : "B",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/persons/bat_3191"
    }
  }
}, {
  "id" : "bat_2340",
  "name" : "B",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/persons/bat_2340"
    }
  }
pdorgambide
  • 1,787
  • 19
  • 33
  • Duplicate : https://stackoverflow.com/questions/45401734/how-to-work-with-dto-in-spring-data-rest-projects – K.Nicholas Jun 18 '19 at 22:30
  • Sorry if I dont explain clear my question but is not duplicate for the proposed. We need remove the relations on _link part of the JSON for all entities to reduce the response size on collection request. I achived it refactoring the PersistentEntityJackson2Module to avoid that Jackson serielize the Links but I am sure that will be a better aproach. I dont know how to do with RestRespositoryMvcConfiguration to modified this aspect. – pdorgambide Jun 19 '19 at 06:11

1 Answers1

0

If I wanted to control the hateos links in springboot I used a resource assembler. As an example:

@Autowired
private EmployeeAddressResourceAssembler assembler;

@GetMapping(value="/{empId}", produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<EmployeeAddressResource> getEmployeeAddress(@PathVariable Integer empId) {
    EmployeeAddressItem employeeAddressItem = restTemplate.getForObject( 
        serviceUrl + "/employee/address/{empId}", 
        EmployeeAddressItem.class, empId);
    return ResponseEntity.ok( assembler.toResource(employeeAddressItem) );
}

And then I used the resource assembler.

@Component
public class EmployeeAddressResourceAssembler
        extends ResourceAssemblerSupport<EmployeeAddressItem, EmployeeAddressResource> {

    public EmployeeAddressResourceAssembler() {
        super(EmployeeAddressController.class, EmployeeAddressResource.class);
    }

    @Override
    public EmployeeAddressResource toResource(EmployeeAddressItem item) {

        // createResource(employeeAddressItem);
        EmployeeAddressResource resource = createResourceWithId(item.getEmpId(), item);
        resource.fromEmployeeAddressItem(item);
        // … do further mapping
        resource.add(linkTo(methodOn(EmployeeAddressController.class).deleteEmployeeAddress(item.getEmpId())).withRel("delete"));        
        return resource;
    }

}

and the ResourceAssembler uses a Resource

public class EmployeeAddressResource extends ResourceSupport {
    private Integer empId;
    private String address1;
    private String address2;
    private String address3;
    private String address4;
    private String state;
    private String country;

    public void fromEmployeeAddressItem(EmployeeAddressItem item) {
        this.empId = item.getEmpId();
        this.address1 = item.getAddress1();
        this.address2 = item.getAddress2();
        this.address3 = item.getAddress3();
        this.address4 = item.getAddress4();
        this.state = item.getState();
        this.country = item.getCountry();
    }

All this at RestPracticeWithHateos

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
  • This aproach works fine with HATEOAS but we need for Spring Data REST where PersistentEntityResourceAssembler, PersistentEntityResource and RepositoryEntityController are the classes that model the resource-link transformation. How to customize the Resource/links for a Entity on Collection Resources returned by Respositories of Spring data REST and defined projections is the goal. – pdorgambide Jun 19 '19 at 20:46