来自AbstractRepositoryRestController#entitiesToResources
来源:
if (page.getContent().isEmpty()) {
return pagedResourcesAssembler.toEmptyResource(page, domainType, baseLink);
}
当页面没有元素时,Spring Data REST会在"content"
节点中放入一个包含无用信息的对象(在我看来),如:
"content": [
{
"relTargetType" : "my.company.ClassName",
"collectionValue" : true,
"value" : [ ]
}
]
"content": []
?page.getContent().isEmpty()
他们解析content
迭代它,并将每个元素映射到某个域实体。由于它不是域实体,因此它们会失败。获取第一个元素并检查是否存在某些特定字段(例如relTargetType
)看起来很脏,不是吗?
答案 0 :(得分:0)
这是我尝试的东西......
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字符串输出:{"nullStr":null,"nullListOrArray":null,"emptyListOrArray":[]}