Home » Java » java – Can I return ResponseEntity<null, HttpStatus> when ResponseEntity<OutputStream, HttpStatus> is expected?-Exceptionshub

java – Can I return ResponseEntity<null, HttpStatus> when ResponseEntity<OutputStream, HttpStatus> is expected?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a spring Controller method that is expected to return ResponseEntity . In one scenario, there isnothing to return. So i am returning ResponseEntity<>(null, HttpStatus.NOT_Found>. However, I am running into some error.

I want to know whether it is valid to return null in this fashion.

How to&Answers:

You can try this following:

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

Answer:

Instead of returning an output stream, you should try returning a byte array.

if (bytearray is not empty) {
    return ResponseEntity<>(byteArray, HttpStatus.OK);
} else {
    return ResponseEntity<>(HttpStatus.NOT_FOUND);
}