Make collection propertie render as relation instead of property in json HAL representaion

前端 未结 1 965
一生所求
一生所求 2020-12-22 01:29

I got hal formatted response as this:

{
  \"name\": \"Publisher A\",
  \"bookPublishers\": [
    {
      \"publishedDate\": \"2019-07-12T08:19:04.583+0000\",         


        
相关标签:
1条回答
  • 2020-12-22 02:14

    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

    0 讨论(0)
提交回复
热议问题