Affects: 2.2.4
That controller works perfectly:
@GetMapping("/test")
public ResponseEntity<StreamingResponseBody> test() {
var response = ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"test.txt\"")
.body(os -> os.write("simple streaming test".getBytes(StandardCharsets.UTF_8)));
return response;
}
But that controller
@GetMapping("/test")
public ResponseEntity<?> test() {
var response = ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"test.txt\"")
.body((StreamingResponseBody)os -> os.write("simple streaming test".getBytes(StandardCharsets.UTF_8)));
return response;
}
produces an exception in response:
{
"timestamp": "2020-10-29T17:46:55.769+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No converter for [class local.pryadko.streamtest.TestControlle$$Lambda$1667/0x0000000840c3c040] with preset Content-Type 'application/octet-stream'",
"path": "/api/test"
}
Looks like Spring can't correctly find out type :neutral_face:
Comment From: poutsma
Because in the second controller, the response entity returned by the method is not bound, so Spring cannot determine that it's a StreamingResponseBody
you're returning.
Comment From: eroux
I'm getting the exact same error, @poutsma can you please elaborate on your answer? it doesn't seem quite straightforward to me...
Comment From: MikePryadko
@eroux, thru time I've changed my mind: this is not “real” problem because using raw generics (<?>
) is not a good practice.
Comment From: eroux
well, I've changed my code too... but if using generics is not properly supported by Spring... why allow it at all?
Comment From: rstoyanchev
@eroux generally speaking, given ResponseEntity<?>
we will take a look at the actual value to determine how to handle it. The problem here is that the response body is a lambda and it is not possible to determine its type. Either the ResponseEntity
has to be declared with the target type or the response body has to be instantiated as a class.