#787 - Return all Link attributes on discovery.

This commit is contained in:
Greg Turnquist
2019-02-06 16:23:16 -06:00
parent 9518a2db9a
commit 9e8e57a74b
13 changed files with 230 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
*/
package org.springframework.hateoas.hal;
import java.util.Map;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
@@ -23,10 +26,35 @@ import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
* {@link LinkDiscoverer} implementation based on HAL link structure.
*
* @author Oliver Gierke
* @author Greg Turnquist
*/
public class HalLinkDiscoverer extends JsonPathLinkDiscoverer {
/**
* Constructor for {@link MediaTypes#HAL_JSON}.
*/
public HalLinkDiscoverer() {
super("$._links..['%s']..href", MediaTypes.HAL_JSON);
super("$._links..['%s']", MediaTypes.HAL_JSON, MediaTypes.HAL_JSON_UTF8);
}
@Override
@SuppressWarnings("unchecked")
protected Link extractLink(Object element, String rel) {
if (element instanceof Map) {
Map<String, String> json = (Map<String, String>) element;
return new Link(json.get("href"), rel)
.withHreflang(json.get("hreflang"))
.withMedia(json.get("media"))
.withTitle(json.get("title"))
.withType(json.get("type"))
.withDeprecation(json.get("deprecation"))
.withProfile(json.get("profile"))
.withName(json.get("name"));
}
return super.extractLink(element, rel);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
*/
package org.springframework.hateoas.hal.forms;
import java.util.Map;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
@@ -26,6 +29,27 @@ import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
public class HalFormsLinkDiscoverer extends JsonPathLinkDiscoverer {
public HalFormsLinkDiscoverer() {
super("$._links..['%s']..href", MediaTypes.HAL_FORMS_JSON);
super("$._links..['%s']", MediaTypes.HAL_FORMS_JSON);
}
@Override
@SuppressWarnings("unchecked")
protected Link extractLink(Object element, String rel) {
if (element instanceof Map) {
Map<String, String> json = (Map<String, String>) element;
return new Link(json.get("href"), rel)
.withHreflang(json.get("hreflang"))
.withMedia(json.get("media"))
.withTitle(json.get("title"))
.withType(json.get("type"))
.withDeprecation(json.get("deprecation"))
.withProfile(json.get("profile"))
.withName(json.get("name"));
}
return super.extractLink(element, rel);
}
}