Hello dear Spring team,
I wonder if my code is buggy or your code is buggy, as I have no other sources to bypass this problem. Trying to write a stream directly to the ServerResponse's of a functional endpoint, but unfortunately it does not work. with the below way.
ServerResponse getResponse (inputRequest){
Item item = new Item("test")
StreamingResponseBody stream = (OutputStream outputStream) -> {
try {
item.visit(JsonEncoder(outputStream));
println("Streaming ended");
} catch (Exception e) {
e.printStackTrace();
}
}
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(stream)
}
Q
Using SpringBoot 6.3.3, Serlvet stack functional endpoints, this lambda is never executed after returned. Is this a bug in your code? Should you support lambdas? Or what is the appropriate way to write directly to ServerResponse's outputStream like the old HttpServletResponse.getOutputStream()? Thank you for your time.
Comment From: bclozel
I don't think this use case was considered in the functional model. I would say that this feature is typically provided by an HttpMessageConverter
that you write once and configure in your application. The StreamingResponseBody
class is bound to the annotation model, so it won't work here by design.
If you would like the Spring Framework team to consider this enhancement, please elaborate on the use case, especially:
- which JSON technology you are using
- whether a custom
HttpMessageConverter
would work for your application and why
Comment From: Odyss-eus
I don't think this use case was considered in the functional model. I would say that this feature is typically provided by an
HttpMessageConverter
that you write once and configure in your application. TheStreamingResponseBody
class is bound to the annotation model, so it won't work here by design.If you would like the Spring Framework team to consider this enhancement, please elaborate on the use case, especially:
* which JSON technology you are using * whether a custom `HttpMessageConverter` would work for your application and why
The use case is that you can create a json via org.codehaus.jackson.JsonGenerator or some other generator of your custom java object. And instead of consuming memory to turn it into String and after return it, you can write it directly in outputStream of the ServerResponse. If it you think it should be an another custom HttpMessageConverter to solve this issue its OK. But I wonder if it can be a built in feature.
Comment From: bclozel
org.codehaus.jackson.JsonGenerator
is ancient and has been replaced by proper Jackson support in Spring Framework.
In this case, I think a custom HttpMessageConverter
is the best choice. Unsupported Spring Framework versions did provide implementations for this Jackson version. Maybe you can write a simplified version of this for your own needs.