Return link 来自 Hateoas
Return link from Hateos
我有这个旧的 Spring Hateos 代码,我想将其迁移到最新版本:
Map<String, Link> links = new HashMap<>();
links.put(Link.REL_NEXT, response.getLink(Link.REL_NEXT));
links.put(Link.REL_PREVIOUS, response.getLink(Link.REL_PREVIOUS));
addLink(url, response, links, Link.REL_NEXT);
addLink(url, response, links, Link.REL_PREVIOUS);
....
private void addLink(String baseUrl, WebResource response, Map<String, Link> links, String rel) {
if (links.get(rel) == null) {
return;
}
Link link = links.get(rel);
String href = baseUrl;
if (link.getHref().contains("?")) {
href = href + link.getHref().substring(link.getHref().indexOf('?'));
}
link = Link.of(href, rel);
response.add(link);
}
我试过这个:
Map<LinkRelation, Optional> links = new HashMap<>();
links.put(IanaLinkRelations.SELF, response.getLink(IanaLinkRelations.SELF));
links.put(IanaLinkRelations.NEXT, response.getLink(IanaLinkRelations.NEXT));
links.put(IanaLinkRelations.PREVIOUS, response.getLink(IanaLinkRelations.PREVIOUS));
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.SELF);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.NEXT);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.PREVIOUS);
private void addLink(String baseUrl, RegistrationsResource response, Map<LinkRelation, Optional> links, LinkRelation rel) {
if (links.get(rel) == null) {
return;
}
Link link = links.get(rel);
String href = baseUrl;
if (link.getHref().contains("?")) {
href = href + link.getHref().substring(link.getHref().indexOf('?'));
}
link = Link.of(href, rel);
response.add(link);
}
我在这一行遇到错误:
Link link = links.get(rel);
Required type: Link
Provided: Optional
你能告诉我实现这个的正确方法是什么吗?
根据您对迁移的评论和问题,这就是我的建议:
Map<LinkRelation, Optional<Link>> links = new HashMap<LinkRelation, Optional<Link>>();
links.put(IanaLinkRelations.SELF, Optional.of(response.getLink(IanaLinkRelations.SELF)));
links.put(IanaLinkRelations.NEXT, Optional.of(response.getLink(IanaLinkRelations.NEXT)));
links.put(IanaLinkRelations.PREVIOUS, Optional.of(response.getLink(IanaLinkRelations.PREVIOUS)));
.....
//调用addLinlk
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.SELF);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.NEXT);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.PREVIOUS);
并在 addLink 中:
private void addLink(String baseUrl, RegistrationsResource response, Map<LinkRelation, Optional> links, LinkRelation rel) {
links.get(rel).ifPresent(x->{
Link link = x;
String href = baseUrl;
if (link.getHref().contains("?")) {
href = href + link.getHref().substring(link.getHref().indexOf('?'));
}
link = Link.of(href, rel);
response.add(link);
});
}
使用 Java 11 & spring-hateoas 1.5.0 测试。如果您有任何不同的版本,请告诉我。
编辑
根据 OP 提到的,他们使用的是 hateoas 版本 2.6.7
。
OP 代码中的 response
引用是自定义 class,它扩展了 RepresentationModel。所以 response.getLink(..)
将 return 类型的 Optional<Link>
。
所以下面的解决方法将起作用:
Map<LinkRelation, Optional<Link>> links = new HashMap<LinkRelation, Optional<Link>>();
links.put(IanaLinkRelations.SELF, response.getLink(IanaLinkRelations.SELF));
...
无需更改 addLink
且无需更改调用 addLink
,我的原始答案对其他操作仍然有效。
我有这个旧的 Spring Hateos 代码,我想将其迁移到最新版本:
Map<String, Link> links = new HashMap<>();
links.put(Link.REL_NEXT, response.getLink(Link.REL_NEXT));
links.put(Link.REL_PREVIOUS, response.getLink(Link.REL_PREVIOUS));
addLink(url, response, links, Link.REL_NEXT);
addLink(url, response, links, Link.REL_PREVIOUS);
....
private void addLink(String baseUrl, WebResource response, Map<String, Link> links, String rel) {
if (links.get(rel) == null) {
return;
}
Link link = links.get(rel);
String href = baseUrl;
if (link.getHref().contains("?")) {
href = href + link.getHref().substring(link.getHref().indexOf('?'));
}
link = Link.of(href, rel);
response.add(link);
}
我试过这个:
Map<LinkRelation, Optional> links = new HashMap<>();
links.put(IanaLinkRelations.SELF, response.getLink(IanaLinkRelations.SELF));
links.put(IanaLinkRelations.NEXT, response.getLink(IanaLinkRelations.NEXT));
links.put(IanaLinkRelations.PREVIOUS, response.getLink(IanaLinkRelations.PREVIOUS));
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.SELF);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.NEXT);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.PREVIOUS);
private void addLink(String baseUrl, RegistrationsResource response, Map<LinkRelation, Optional> links, LinkRelation rel) {
if (links.get(rel) == null) {
return;
}
Link link = links.get(rel);
String href = baseUrl;
if (link.getHref().contains("?")) {
href = href + link.getHref().substring(link.getHref().indexOf('?'));
}
link = Link.of(href, rel);
response.add(link);
}
我在这一行遇到错误:
Link link = links.get(rel);
Required type: Link
Provided: Optional
你能告诉我实现这个的正确方法是什么吗?
根据您对迁移的评论和问题,这就是我的建议:
Map<LinkRelation, Optional<Link>> links = new HashMap<LinkRelation, Optional<Link>>();
links.put(IanaLinkRelations.SELF, Optional.of(response.getLink(IanaLinkRelations.SELF)));
links.put(IanaLinkRelations.NEXT, Optional.of(response.getLink(IanaLinkRelations.NEXT)));
links.put(IanaLinkRelations.PREVIOUS, Optional.of(response.getLink(IanaLinkRelations.PREVIOUS)));
.....
//调用addLinlk
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.SELF);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.NEXT);
addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.PREVIOUS);
并在 addLink 中:
private void addLink(String baseUrl, RegistrationsResource response, Map<LinkRelation, Optional> links, LinkRelation rel) {
links.get(rel).ifPresent(x->{
Link link = x;
String href = baseUrl;
if (link.getHref().contains("?")) {
href = href + link.getHref().substring(link.getHref().indexOf('?'));
}
link = Link.of(href, rel);
response.add(link);
});
}
使用 Java 11 & spring-hateoas 1.5.0 测试。如果您有任何不同的版本,请告诉我。
编辑
根据 OP 提到的,他们使用的是 hateoas 版本 2.6.7
。
OP 代码中的 response
引用是自定义 class,它扩展了 RepresentationModel。所以 response.getLink(..)
将 return 类型的 Optional<Link>
。
所以下面的解决方法将起作用:
Map<LinkRelation, Optional<Link>> links = new HashMap<LinkRelation, Optional<Link>>();
links.put(IanaLinkRelations.SELF, response.getLink(IanaLinkRelations.SELF));
...
无需更改 addLink
且无需更改调用 addLink
,我的原始答案对其他操作仍然有效。