Skip to content

AbstractJackson2Encoder::getContentLength should not return null for MonoJust/FluxJust publisher [SPR-16892] #21431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jun 1, 2018 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 1, 2018

Mirko Adebahr opened SPR-16892 and commented

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.


Affects: 5.0.6

Issue Links:

Referenced from: commits 0103521, 4a26f93, 7bfd683, d1c9401

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented 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 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.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jun 4, 2018

Mirko Adebahr commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Ok so just to be clear, if we ensured Content-Length for Mono, that would solve your needs?

@spring-projects-issues
Copy link
Collaborator Author

Mirko Adebahr commented

Yes, absolutely.

@privettoli
Copy link

privettoli commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants