简体   繁体   中英

Can Spring Data Rest use _links per HAL spec?

A POST request with media type, "application/hal+json", is issued to a Spring Data Rest endpoint, /api/groupMembers . It contains the following payload:

{
  "id":null,
  "_links":{
    "group":{"href":"http://local:8080/api/groups/7"},
    "item":{"href":"http://local:8080/api/items/10689"},
    "groupItem":{"href":"http://local:8080/api/items/10689"}
  }
}

This is similar to the example provided in the HAL specification . All of these URIs point to existing resources and can be dereferenced in a web browser.

However, these Link Objects do not appear to be assembled correctly by Spring Data Rest on the server:

TRACE 20248 --- [nio-8080-exec-9] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: [org.springframework.data.rest.webmvc.RootResourceInformation@15aacfd5, Resource { content: GroupMember(id=null, group=null, item=null, groupItem=null), links: [] }, org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler@1d8f0f1a, application/hal+json]

Why doesn't Spring Data Rest populate these properties ( group , item , groupItem ) using the Link Objects?

Update

A GET request to /api/GroupMembers/1 produces:

{
  "id":1,
  "_links":{
    "group":{"href":"http://local:8080/api/groups/1"},
    "item":{"href":"http://local:8080/api/items/689"},
    "groupItem":{"href":"http://local:8080/api/items/689"}
  }
}

So, Spring Data Rest can produce/serialize this JSON HAL response but cannot handle (deserialize) the same JSON HAL in a request.

Update 2

The GroupMember entity is:

@Entity
@Data
public class GroupMember {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne
    @JoinColumn(name="groupId")
    private Group group;
    
    @ManyToOne
    @JoinColumn(name="itemId")
    private Item item;
    
    @ManyToOne
    @JoinColumn(name="groupItemId")
    private Item groupItem;
    
}

I believe what you are trying to achieve is something like this:

POST /api/groupMembers

{
  "id":null,
  "group": "http://local:8080/api/groups/7",
  "item": "http://local:8080/api/items/10689",
  "groupItem": "http://local:8080/api/items/10689"
}

_links are just metadata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM