How to disable wrapping collections with dependencies (in my case Set) in the links in the response body? explanation below.

There are entities with @OneToMany and @ManyToOne connections. There is a repository inherited from JpaRepository. The application is on SpringBut, everything works on a spring date. That is, the controllers and services are implemented by spring itself. For clarity, below classes Owner and Cat

@Entity public class Cat{ @Id Long Id; String catName; @ManyToOne(...) Owner owner; 

Class hosts

 @Entity public class Owner{ @Id Long Id; String ownerName; @OneToMany(...) Set<Cat> pets; 

Everything works, classes are created, the cats are drawn into the set. In JSON format, the output looks like this:

 {"id":"1", "catName":"Barsik", "owner":"Valera"} 

and the owner

 {"id":"1", "ownerName":"Valera", "pets":[{"id":"1", "catName":"Barsik", "owner":"Valera"}]} 

BUT (!) Spring Data provides data in the HAL + JSON format, and the host output looks like this

 {"id":"1", "ownerName":"Valera", "_links" : { "self" : { "href" : "http://localhost:8080/owners/1" }, "lot" : { "href" : "http://localhost:8080/owners/1" }, "pets" : { "href" : "http://localhost:8080/owners/1/pets"} }} 

I am very grateful to the spring for the care, and the fact that he built the rest for me, but I need to work with the model when the cat is invested in the owner in one JSONe.

QUESTION: How to disable wrapping collections with dependencies (in my case Set) into links in the response body?

    2 answers 2

    You can use projections ( Projection ).

    For example, it was like this:

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

    After adding a projection

     @Projection(name = "inlineAddress", types = { Person.class }) interface InlineAddress { String getFirstName(); String getLastName(); Address getAddress(); } 

    It became so:

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

    To get the result with the projection, you need to add the projection parameter to the request:

     http://localhost:8080/persons/1?projection=inlineAddresses 

    If you connect the projection to the default repository (called Excerpt ), for example:

     @RepositoryRestResource(excerptProjection = InlineAddress.class) interface PersonRepository extends CrudRepository<Person, Long> {} 

    This parameter can not be added to the request. But this is true only for lists. To obtain a single object, you must still specify the projection parameter.

    To disable HAL, you can use these recommendations from the author of Spring Data REST.

      You need to mark your interface, inherited from JpaRepository (or something similar) as @RepositoryRestResource(exported = false) .


      Original answer .

      • Then rest is not built on this repository. After all, neither the controllers nor the services are implemented by me. I'm interested in some kind of config or something like that, which disables wrapping content in a link inside ResponceBody. That is, do not turn off HAL completely, but only bring the collections to the body of the answer - Snowy