简体   繁体   English

restTemplate.getForObject(“URL”,Object [] .class)可以返回NULL吗?

[英]Can restTemplate.getForObject(“URL”, Object[].class) return NULL?

I have used the solution from this answer: Get list of JSON objects with Spring RestTemplate It works perfectly. 我使用了这个答案的解决方案: 使用Spring RestTemplate获取JSON对象列表它完美地运行。 It doing exactly what I need. 它完全符合我的需要。

ProcessDefinition[] response = restTemplate.getForObject(url, ProcessDefinition[].class);

Is it enought: 是否应该:

return Arrays.asList(response);

or will be better this way: 或者这样会更好:

return Arrays.asList(Optional.ofNullable(response).orElse(new ProcessDefinition[0]));

PS Sorry for starting the new topic, but my karma does not allow me to comment the answer. PS很抱歉开始新主题,但我的业力不允许我评论答案。

Yes, the result of 是的,结果

ProcessDefinition[] response = restTemplate.getForObject(url, ProcessDefinition[].class);

can be null if HTTP response was empty (not [] , but totally empty). 如果HTTP响应为空(不是[] ,但是完全为空),则可以为null

So it is safer to check it if you are not sure that HTTP response never be empty. 因此,如果您不确定HTTP响应永远不会为空,那么检查它会更安全。

return Optional.ofNullable(response).map(Arrays::asList).orElseGet(ArrayList::new)

or 要么

return Optional.ofNullable(response).map(Stream::of).orElseGet(Stream::empty)

if you need a stream. 如果你需要一个流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 restTemplate.getForObject上的HttpClientErrorException - HttpClientErrorException on restTemplate.getForObject 在 Android 中使用 restTemplate.getForObject 反序列化 JSON object 的正确方法 - Proper way to use restTemplate.getForObject to deserialize JSON object in Android 没有获得通过RestTemplate.getForObject传递的标头 - not getting headers passed with RestTemplate.getForObject Spring Boot 微服务 - 将授权标头添加到 restTemplate.getForObject - Spring Boot Microservices - Add authorization header to restTemplate.getForObject 如何对 Mediatypes Rest 端点进行 restTemplate.getForObject 调用 - How to make restTemplate.getForObject call for Mediatypes Rest endpoint Spring restTemplate.getForObject()的JSON转换错误 - JSON conversion error from Spring restTemplate.getForObject() 调用restTemplate.getForObject时如何处理未找到数据 - How to handle no data found when calling restTemplate.getForObject 将RestTemplate的getForObject特殊字符转换为URL参数 - RestTemplate getForObject special characters into URL parameters Spring RESTTemplate getForObject方法返回的内容类型不为“ null” - Content type 'null' not supported returned by Spring RESTTemplate getForObject method 使用RestTemplate getForObject方法 - Using RestTemplate getForObject method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM