#128 - Severe refactoring regarding LinkDiscoverers.

The LinkDiscoverer interface is now a Plugin<MediaType> to allow selecting a discoverer per media type. We also provide a LinkDiscoverers wrapper around a PluginRegistry for easier injection.

Dropped DefaultLinkDiscoverer as the above mentioned changed prohibits the usage of a discoverer without a media type. Clients that previously used DefaultLinkDiscoverer should switch to JsonPathLinkDiscoverer with a custom JsonPath expression and media type.

@EnableHypermediaSupport now supports multiple HypermediaType values set to be able to set up support for multiple hypermedia formats. Admittedly, the only currently supported type is HAL but we prepare for the addition of other (e.g. Collection/JSON or the like). We changed the infrastructure setup taken care of by @EnableHypermediaSupport to not enrich all ObjectMapper instances with our HAL modules but explicitly register an ObjectMapper with the application context and then registering a custom MappingJackson(2)HttpMessageConverter with the HandlerAdapter instances present in the ApplicationContext. We skip the registration if an already registered MJ2HMC has an ObjectMapper already supporting the Spring HATEOAS mixins.

Removed some compiler warnings.
This commit is contained in:
Oliver Gierke
2013-12-18 10:34:19 +01:00
parent 9eacd07203
commit 137e7c390f
17 changed files with 356 additions and 151 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.hateoas.hal;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
@@ -26,6 +27,6 @@ import org.springframework.hateoas.core.JsonPathLinkDiscoverer;
public class HalLinkDiscoverer extends JsonPathLinkDiscoverer {
public HalLinkDiscoverer() {
super("$_links..%s.href");
super("$_links..%s.href", MediaTypes.HAL_JSON);
}
}

View File

@@ -48,6 +48,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
@@ -83,6 +84,18 @@ public class Jackson2HalModule extends SimpleModule {
setMixInAnnotation(Resources.class, ResourcesMixin.class);
}
/**
* Returns whether the module was already registered in the given {@link ObjectMapper}.
*
* @param mapper must not be {@literal null}.
* @return
*/
public static boolean isAlreadyRegisteredIn(ObjectMapper mapper) {
Assert.notNull(mapper, "ObjectMapper must not be null!");
return LinkMixin.class.equals(mapper.findMixInClassFor(Link.class));
}
/**
* Custom {@link JsonSerializer} to render Link instances in HAL compatible JSON.
*

View File

@@ -22,15 +22,22 @@ import javax.xml.bind.annotation.XmlElement;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;
@SuppressWarnings("deprecation")
abstract class ResourceSupportMixin extends ResourceSupport {
@Override
@XmlElement(name = "link")
@org.codehaus.jackson.annotate.JsonProperty("_links")
@com.fasterxml.jackson.annotation.JsonProperty("_links")
@org.codehaus.jackson.map.annotate.JsonSerialize(include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_EMPTY, using = org.springframework.hateoas.hal.Jackson1HalModule.HalLinkListSerializer.class)
@org.codehaus.jackson.map.annotate.JsonDeserialize(using = org.springframework.hateoas.hal.Jackson1HalModule.HalLinkListDeserializer.class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(include = com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion.NON_EMPTY, using = org.springframework.hateoas.hal.Jackson2HalModule.HalLinkListSerializer.class)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = org.springframework.hateoas.hal.Jackson2HalModule.HalLinkListDeserializer.class)
@org.codehaus.jackson.map.annotate.JsonSerialize(
include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_EMPTY,
using = org.springframework.hateoas.hal.Jackson1HalModule.HalLinkListSerializer.class)
@org.codehaus.jackson.map.annotate.JsonDeserialize(
using = org.springframework.hateoas.hal.Jackson1HalModule.HalLinkListDeserializer.class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(
include = com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion.NON_EMPTY,
using = org.springframework.hateoas.hal.Jackson2HalModule.HalLinkListSerializer.class)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(
using = org.springframework.hateoas.hal.Jackson2HalModule.HalLinkListDeserializer.class)
public abstract List<Link> getLinks();
}

View File

@@ -21,16 +21,23 @@ import javax.xml.bind.annotation.XmlElement;
import org.springframework.hateoas.Resources;
@SuppressWarnings("deprecation")
public abstract class ResourcesMixin<T> extends Resources<T> {
@Override
@XmlElement(name = "embedded")
@org.codehaus.jackson.annotate.JsonProperty("_embedded")
@com.fasterxml.jackson.annotation.JsonProperty("_embedded")
@org.codehaus.jackson.map.annotate.JsonSerialize(include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_EMPTY, using = org.springframework.hateoas.hal.Jackson1HalModule.HalResourcesSerializer.class)
@org.codehaus.jackson.map.annotate.JsonDeserialize(using = org.springframework.hateoas.hal.Jackson1HalModule.HalResourcesDeserializer.class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(include = com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion.NON_EMPTY, using = org.springframework.hateoas.hal.Jackson2HalModule.HalResourcesSerializer.class)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = org.springframework.hateoas.hal.Jackson2HalModule.HalResourcesDeserializer.class)
@org.codehaus.jackson.map.annotate.JsonSerialize(
include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_EMPTY,
using = org.springframework.hateoas.hal.Jackson1HalModule.HalResourcesSerializer.class)
@org.codehaus.jackson.map.annotate.JsonDeserialize(
using = org.springframework.hateoas.hal.Jackson1HalModule.HalResourcesDeserializer.class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(
include = com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion.NON_EMPTY,
using = org.springframework.hateoas.hal.Jackson2HalModule.HalResourcesSerializer.class)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(
using = org.springframework.hateoas.hal.Jackson2HalModule.HalResourcesDeserializer.class)
public abstract Collection<T> getContent();
}