Why is an object wrapped in a content object with Spring Data REST when accessed via an association link?

Using Spring Data REST with repositories JPARepsitory

, I have two entities that have a one-to-many relationship.

@Entity
class Owner {

    // owner attributes...

    @OneToMany
    Set<Item> items;
}

      

and

@Entity
class Item {

    // item attributes...

    @ManyToOne
    Owner owner;
}    

      

The owner setting with ID 1 has an element with ID 5.

When I call http://localhost:8080/api/items/5

I get something like

{
  // item attributes
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/items/5"
    },
    "item": {
      "href": "http://localhost:8080/api/items/5"
    },
    "owner": {
      "href": "http://localhost:8080/api/items/5/owner"
    }
  }
}

      

Then when I call http://localhost:8080/api/items/5/owner

I get

{
  "content": {
    // owner attributes
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/owners/1"
    },
    "owner": {
      "href": "http://localhost:8080/api/owners/1"
    },
    "items": {
      "href": "http://localhost:8080/api/owners/1/items"
    }
  }
}

      

However, if I call http://localhost:8080/api/owners/1

(which is the same entity, but instead of id href instead of href), I get something like

{
  // owner attributes
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/owners/1"
    },
    "owner": {
      "href": "http://localhost:8080/api/owners/1"
    },
    "items": {
      "href": "http://localhost:8080/api/owners/1/items"
    }
  }
}

      

Here the owner is wrapped with an additional object content

when called from the unique resource of the association.

Update to clarify

I would expect that if I call the self

href, I will return to the same view, but in this case I will not. I am getting a canonical entity view.

My question is, why is this so? If the object returned through the association resource has an self

href that is the URI the object was retrieved from, or if the object returned through the association resource has the same representation as the entity element resource?

In short, when I call http://localhost:8080/api/items/5/owner

, I expect to receive

{
  "content": {
    // owner attributes
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/items/5/owner"
      // ^^^ the URI that was retrieved that will always return this representation
    },
    "owner": {
      "href": "http://localhost:8080/api/owners/1"
      // ^^^ the canonical entity URI
    },
    "items": {
      "href": "http://localhost:8080/api/owners/1/items"
    }
  }
}

      

OR

{
  // owner attributes
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/owners/1"
    },
    "owner": {
      "href": "http://localhost:8080/api/owners/1"
    },
    "items": {
      "href": "http://localhost:8080/api/owners/1/items"
    }
  }
}
// ^^^ The canonical entity representation (no "content" wrapper)

      

but not a combination of the two.

+3


source to share


1 answer


http://localhost:8080/api/items/5/owner

refers to the so-called association resource . It does not point to the "real" resource of the owner.

The goal is to handle associations. For example. sending a request with DELETE

will remove the owner from the object, but not remove the actual owner.



Depending on the configuration, you can get all the built-in owner attributes. This is what you get as content

.

+1


source







All Articles