1

I got hal formatted response as this:

{
  "name": "Publisher A",
  "bookPublishers": [
    {
      "publishedDate": "2019-07-12T08:19:04.583+0000",
      "_links": {
        "publisher": {
          "href": "http://localhost:8080/api/publishers/1"
        },
        "book": {
          "href": "http://localhost:8080/api/books/2"
        }
      }
    },
    {
      "publishedDate": "2019-07-12T08:19:04.564+0000",
      "_links": {
        "publisher": {
          "href": "http://localhost:8080/api/publishers/1"
        },
        "book": {
          "href": "http://localhost:8080/api/books/1"
        }
      }
    }
  ],
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/publishers/1"
    },
    "publisher": {
      "href": "http://localhost:8080/api/publishers/1"
    },
    "friends": {
      "href": "http://localhost:8080/api/publishers/1/friends"
    },
    "createdBy": {
      "href": "http://localhost:8080/api/publishers/1/contact"
    }
  }
}

I see there property bookPublishers and also in links friends. Imho they should be both association links (see 2.4. Creating the Associations) where can I "put" another resources.

I would like to make spring render bookPublishers same as friends.

Sample project is here: https://github.com/luvarqpp/poc-springHalRelations

You can do:

git clone https://github.com/luvarqpp/poc-springHalRelations.git
cd poc-springHalRelations
mvn clean spring-boot:run

And than open http://localhost:8080/api

PS: Bonus question, what is easiest way to provide own relation for business logic, like relation "renameAuthor" for example.

Lubo
  • 1,621
  • 14
  • 33
  • 1
    `bookPublishers` are in-lined in the response because you have no repository for BookPublisher. If you want a link then create a repository. As for creating addiitonal links see: https://stackoverflow.com/questions/23135756/how-to-add-links-to-root-resource-in-spring-data-rest/24791083#24791083 – Alan Hay Jul 12 '19 at 11:15

1 Answers1

2

For collection relationships, Spring Data will provide a link when a repository exists for the relevant type. Where no repository exists then the collection will be in-lined in the response, otherwise, how else will the client get the data.

Therefore, create a repository for your BookPublisher type.

Relevant documentation part citation:

the component responsible for creating the links to referenced entities (such as those objects under the _links property in the object’s JSON representation). It takes an @Entity and iterates over its properties, creating links for those properties that are managed by a Repository and copying across any embedded or simple properties.

You can also create a projection that would in-line the data when required. Clients could specify this projection in the request therefore preventing an additional server call.

e.g.

/publisher/1?projection=withBookPublishers.

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

Lubo
  • 1,621
  • 14
  • 33
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • Is it possible to have associative relation in HAL response included (explicitly) also if I made completely detached entities? (i.e. Publisher class with no Book reference and viceversa; but separate repository with BookPublisher, which adds association to them) – Lubo Jul 14 '19 at 07:42
  • This solution alone does not work for IdClass approach, because multiple keys are not supported by spring-rest (at least not out of box). See https://jira.spring.io/browse/DATAREST-846 and https://stackoverflow.com/a/31830586/11152683 – Lubo Sep 04 '19 at 06:47
  • I have implemented this solution into branch of my example. https://github.com/luvarqpp/poc-springHalRelations/tree/solution/byAlanHay Currently generation of JSON+HAL is nearly ideal, but JPA/Hibernate mapping does limit manyToMany with unique constraint (for booth parts of mapping separately) in associative table. This is issue with JPA, for another question. – Lubo Sep 05 '19 at 12:59