Description
Jackson is better at serializing arrays than lists since there is no type erasure. When a GET method returns a Resources
object, I see something like:
{
"_embedded": {
"hashMapList": [
{
"fld1": "a",
"_links": {
"self": {
"href": "http://..."
}
}
}
]
}
}
The "hashMapList" sticks out. I would rather see this:
{
"content": [
{
"fld1": "a",
"_links": {
"self": {
"href": "http://..."
}
}
}
]
}
In retrospect, Resources.getContents() should have probably returned T[]
instead of Collection<T>
.
My workaround was to create a class called ArrayResources that extended Resources with the following code:
@Override
@JsonIgnore
public Collection<T> getContent() {
return super.getContent();
}
@XmlAnyElement
@XmlElementWrapper
@JsonProperty("content")
@SuppressWarnings("unchecked")
public T[] getContentArray() {
Collection<T> content = getContent();
return content == null ? null :
(T[]) content.toArray();
}
I also had to write ArrayPagedResources that duplicated most of the code in PagedResources. Finally I provided static initializers that I use in my Controller classes right before returning.
ArrayResources.from(Resources)
PagedArrayResources.from(PagedResources)
Not the most elegant solution, but it got me what I want. Ideally, I would like to see the code for Resources changed as shown above. If maintaining backward compatibility is vital, perhaps a mixin similar to ResourcesMixin could provide this as an optional feature. (I tried to do this myself, but I never got it to work.) If a mixin could be developed perhaps an attribute would be appropriate, e.g., @EnableResourcesArraySerialization
.
If there is an easier way to accomplish this that I am missing, I welcome suggestions. Kudos for all your great work.