[英]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.