简体   繁体   English

如何使用Spring HATEOAS发布和发布链接

[英]How to put and post links with Spring HATEOAS

I'm trying to understand how to create and modify links in Spring HATEOAS. 我正在尝试了解如何在Spring HATEOAS中创建和修改链接。

For example, say I have two collections, one at api/users and another at api/event. 例如,假设我有两个集合,一个在api / users,另一个在api / event。 I would like to associate a user api/user/56 with an event api/event/21. 我想将用户api / user / 56与事件api / event / 21相关联。 For arguments sake this is a many-to-many - a user may attend many events, an event may have many users. 为了论证,这是多对多的 - 用户可以参加许多活动,一个活动可能有很多用户。

As I understand it, the restful way of doing this is to use the URIs as primary keys, so I might post the following to api/user/56/events; 据我了解,这样做的另一种方法是使用URI作为主键,因此我可以将以下内容发布到api / user / 56 / events;

{
    attends: "http://localhost:9090/api/event/21"
}

The endpoint then needs to be able to parse that URL and extract the ID (in this case 21) and the controller (EventController.class) so that I can persist this. 然后端点需要能够解析该URL并提取ID(在本例中为21)和控制器(EventController.class),以便我可以持久保存。

Question 1: Is this the correct way of dealing with relationships in Spring Hateoas in terms of the REST API? 问题1:这是在REST API方面处理Spring Hateoas中的关系的正确方法吗?

Question 2: How can I resolve this url in a controller to a usable handle on the data (for example a reference to the appropriate controller/method, a primary key, etc) 问题2:如何将控制器中的此URL解析为数据的可用句柄(例如,对相应控制器/方法的引用,主键等)

Research 研究

RestTemplate can be used to request the data from the controller inside the request mapped method, like so; RestTemplate可用于从请求映射方法内的控制器请求数据,如此;

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<EventResource> response = restTemplate.getForEntity(attendsUrl, EventResource.class);
EventResource eventResource = response.getBody();

However I don't believe that eventResource should return an Id field as part of the data - it's not very restful and this would be exposed on the API. 但是我不相信eventResource应该返回一个Id字段作为数据的一部分 - 它不是很安静,这将在API上公开。 One approach is to have a parameter "includePK=true" but again this doesn't feel right - it's just hiding the problem. 一种方法是使参数“includePK = true”,但这感觉不对 - 它只是隐藏了问题。 Moreover the idea of the server making requests to it's own API in this manner seems circuitous. 此外,服务器以这种方式向它自己的API发出请求的想法似乎很迂回。

Update 更新

There is an open question for this here https://github.com/spring-projects/spring-hateoas/issues/292 . 这里有一个未解决的问题https://github.com/spring-projects/spring-hateoas/issues/292 Based loosely on some of the comments (by user kevinconaway ) from that issue I have made a quick util class that offers an easy solution here: SpringHateoasUtils . 基于该问题的一些评论(由用户kevinconaway )松散地我已经创建了一个快速的util类,它提供了一个简单的解决方案: SpringHateoasUtils The solution boils down to; 解决方案归结为;

String mapping = DISCOVERER.getMapping(targetClass, targetMethod);
UriTemplate template = new UriTemplate(mapping);
//values is key/value map of parameters that the referenced method accepts
Map<String, String> values = uriTemplate.match(uri);

SpringHateoasUtils makes this slightly nicer but it still feels like it should be a feature. SpringHateoasUtils使这个稍微好一点,但它仍然觉得它应该是一个功能。 I'll seek to get something in the spring code for this - when it's clear what is happening with this I'll answer this question. 我将寻求在春季代码中获得一些东西 - 当它清楚地发生了什么时,我会回答这个问题。

Look at the answer here: 看看这里的答案:

POSTing a @OneToMany sub-resource association in Spring Data REST 在Spring Data REST中发布@OneToMany子资源关联

Question 1) Yes this is how you post links/relations. 问题1)是的,这是您发布链接/关系的方式。 With URIs. 使用URI。

Question 2) The URI of the resource actually IS its ID from the client's perspective. 问题2)从客户的角度来看,资源的URI实际上它的ID。 The server internally automatically resolves this URI into the actual model instance with 服务器在内部自动将此URI解析为实际的模型实例

org.springframework.data.rest.core.UriToEntityConverter.convert(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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