4

I have used the solution from this answer: Get list of JSON objects with Spring RestTemplate It works perfectly. 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]));

P.S. Sorry for starting the new topic, but my karma does not allow me to comment the answer.

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
virtuemaster
  • 55
  • 1
  • 8

1 Answers1

3

Yes, the result of

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

can be null if HTTP response was empty (not [], but totally empty).

So it is safer to check it if you are not sure that HTTP response never be empty.

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.

Ruslan Stelmachenko
  • 3,746
  • 28
  • 43