You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AbstractJackson2Encoder::getContentLength should not return null for MonoJust/FluxJust publisher [SPR-16892] #21431
We're trying to use WebClient for creating a non-blocking POST request. The request results in a 400 Bad Request due to a missing content-length header. As the requested endpoint belongs to a 3rd party application, we cannot change that server's behaviour.
In #21085, the EncoderHttpMessageWriter was enhanced to automatically set the content-length Header if the provided publisher is a Mono. Unfortunately, this does not help when using Jackson2JsonEncoder for encoding as getContentLength will always return null.
In my understanding, for MonoJusts and FluxJusts it should be possible to determine the content length and set the header value accordingly.
For Jackson we'd have to buffer the serialized content in memory before writing anything out, so the trade-off is between having Content-Length and requiring less memory.
For Mono it would be straight-forward to set Content-Length always regardless of MonoJust or not. In fact looking back at the solution for #21085 I don't even think we need a getContentLength method on Encoder in order to do that.
For Flux we can't do this automatically since it can impact how much memory is used to buffer the serialized content. What we can do is expose a property like bufferForContentLength or something similar.
For Mono it would be straight-forward to set Content-Length always regardless of MonoJust or not. In fact looking back at the solution for #21085 I don't even think we need a getContentLength method on Encoder in order to do that.
As this would solve our specific issue it would be perfectly sufficient. I just took it for granted that the encoder has to provide the content length thus the issue title.
Still, it is possible that the alternative options you mentioned could be valuable for other users - I myself found it difficult to understand why some Encoder implementations do support getContentLength while others don't.
Activity
spring-projects-issues commentedon Jun 4, 2018
Rossen Stoyanchev commented
For Jackson we'd have to buffer the serialized content in memory before writing anything out, so the trade-off is between having Content-Length and requiring less memory.
For
Monoit would be straight-forward to set Content-Length always regardless ofMonoJustor not. In fact looking back at the solution for #21085 I don't even think we need agetContentLengthmethod onEncoderin order to do that.For
Fluxwe can't do this automatically since it can impact how much memory is used to buffer the serialized content. What we can do is expose a property likebufferForContentLengthor something similar.spring-projects-issues commentedon Jun 4, 2018
Mirko Adebahr commented
As this would solve our specific issue it would be perfectly sufficient. I just took it for granted that the encoder has to provide the content length thus the issue title.
Still, it is possible that the alternative options you mentioned could be valuable for other users - I myself found it difficult to understand why some
Encoderimplementations do supportgetContentLengthwhile others don't.spring-projects-issues commentedon Jun 4, 2018
Rossen Stoyanchev commented
Ok so just to be clear, if we ensured Content-Length for Mono, that would solve your needs?
spring-projects-issues commentedon Jun 4, 2018
Mirko Adebahr commented
Yes, absolutely.
privettoli commentedon Mar 22, 2020
@rstoyanchev would it be possible to implement similar behavior for MultipartHttpMessageWriter? Similar use-case, breaking compatibility with legacy service when migrating from RestTemplate to WebClient. It seems like the receiving legacy service is running on top of PHP and FastCGI and has this problem https://bugs.php.net/bug.php?id=60826.