Open spring-projects-issues opened 5 years ago
Ben Madore commented
People have been running into this on Stack Overflow for a few years, and there has been no response from anyone:
David Lopez commented
I'm puzzled to see that this issue is reported and has not been fixed so far.
IMHO, it is quite blocking as returning an empty list of resources is a quite usual case.
I'm not aware of any good workaround; all that I have found either does not work or is extremely tricky (and unportable).
Sadly, I'll have to remove Spring Data REST from the project; it was promising, but I don't know how I could handle correctly this situation.
I read that Resources
class handles this situation, but it seems that the latest versions of Spring HATEOAS remove these classes and remove them by EntityModel
/CollectionModel
which fails in the de-serialization
David Lopez commented
Finally, I had to adapt the solution provided in the previous links to use the types introduced by the Spring HATEOAS 1.x.
@Component
public class ResourceProcessorEmpty implements RepresentationModelProcessor<CollectionModel<Object>> {
@Override
public CollectionModel<Object> process(CollectionModel<Object> resourceToThrowAway) {
if (resourceToThrowAway.getContent().size() != 1) {
return resourceToThrowAway;
}
if (!resourceToThrowAway.getContent().iterator().next().getClass().getCanonicalName().contains("EmptyCollectionEmbeddedWrapper")) {
return resourceToThrowAway;
}
CollectionModel<Object> newResource = new CollectionModel<>(Collections.emptyList());
newResource.add(resourceToThrowAway.getLinks());
return newResource;
}
}
The above workaround works and ensures Spring Data Rest returns an empty list in the 'content' element instead of a 1 element array.
Returning an array with a single element is very strange when there are no rows in the datasource..
MitchelLabonte opened DATAREST-1346 and commented
When setting the default media type to application/json, an empty collection will be serialized like this:
This happens because class AbstractRepositoryRestController, method entitiesToResources maps empty collections to an EmptyCollectionEmbeddedWrapper no matter what media type is used. But, the custom deserializer for EmptyCollectionEmbeddedWrapper is only used when using hal as the mediatype
Affects: 3.1.5 (Lovelace SR5)