#1157 - Tweaks to EntityModel to allow deserialization with Map<String, Object> content.
We now keep internal methods in EntityModel that are configured using Jackson annotations so that it'd populate the content with a Map in case Map<String, Content> is defined as payload type.
This commit is contained in:
@@ -17,10 +17,13 @@ package org.springframework.hateoas;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
|
||||
/**
|
||||
@@ -31,7 +34,7 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
*/
|
||||
public class EntityModel<T> extends RepresentationModel<EntityModel<T>> {
|
||||
|
||||
private final T content;
|
||||
private T content;
|
||||
|
||||
/**
|
||||
* Creates an empty {@link EntityModel}.
|
||||
@@ -75,6 +78,25 @@ public class EntityModel<T> extends RepresentationModel<EntityModel<T>> {
|
||||
return content;
|
||||
}
|
||||
|
||||
// Hacks to allow deserialization into an EntityModel<Map<String, Object>>
|
||||
|
||||
@JsonAnySetter
|
||||
private void setPropertiesAsMap(String key, Object value) {
|
||||
getOrInitAsMap().put(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> getOrInitAsMap() {
|
||||
|
||||
if (this.content == null) {
|
||||
this.content = (T) new LinkedHashMap<>();
|
||||
} else {
|
||||
Assert.state(Map.class.isInstance(this.content), "Content is not a Map!");
|
||||
}
|
||||
|
||||
return (Map<String, Object>) this.content;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.ResourceSupport#toString()
|
||||
|
||||
Reference in New Issue
Block a user