3

From the AbstractRepositoryRestController#entitiesToResources source:

if (page.getContent().isEmpty()) {
    return pagedResourcesAssembler.toEmptyResource(page, domainType, baseLink);
}

When a page has no elements, Spring Data REST puts into the "content" node an object with useless information (on my point of view) like:

"content": [
    {
        "relTargetType" : "my.company.ClassName",
        "collectionValue" : true,
        "value" : [ ]
    }
] 
  1. What's the idea/purpose of providing that information?
  2. Is there any way to return an empty array "content": [] when page.getContent().isEmpty()?
  3. If it isn't possible, how then should clients handle such an unexpected format?

They parse the content iterating over it, and map each element to some domain entity. Since it isn't a domain entity, they fail. Fetching the first element and checking it on the existence of some specific fields (e.g. relTargetType) looks dirty, doesn't it?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142

1 Answers1

0

Here is something what i tried...

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONObject;

public class TEST {

    public static void main(String[] args) {
        String nullStr = null;
        List<String> nullListOrArray = null;
        List<String> emptyListOrArray = new ArrayList<>();

        JSONObject json = new JSONObject();
        json.put("nullStr", nullStr);
        json.put("nullListOrArray", nullListOrArray);
        json.put("emptyListOrArray", emptyListOrArray);

        System.out.println(json.toString());
    }

}

Json string output: {"nullStr":null,"nullListOrArray":null,"emptyListOrArray":[]}

Vandit Upadhyay
  • 315
  • 3
  • 8