Skip to content
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

Improve handling of requests with an empty body and no content type to match Spring MVC [SPR-15758] #20313

Closed
spring-issuemaster opened this issue Jul 10, 2017 · 4 comments

Comments

@spring-issuemaster
Copy link
Collaborator

commented Jul 10, 2017

Andy Wilkinson opened SPR-15758 and commented

Using Spring MVC, the request body for a request with an empty body and no content type can be handled by a controller method that uses @RequestBody(required = false). As discussed with Rossen, this is thanks to the noContentType flag in AbstractMessageConverterMethodArgumentResolver. No such flag exists in WebFlux and, as a result, a request with an empty body and no content type is rejected with a 415 response. Ideally, an empty body and no content type would work with both the annotation and functional programming models supported by WebFlux.

For the annotation model, it would look like this:

@PostMapping("/testing")
public String testing(@RequestBody(required = false) Map<String, String> body) {
	return "It worked: " + body;
}

For the functional web framework, it might look something like this:

private static final ParameterizedTypeReference<Map<String, String>> BODY_TYPE = new ParameterizedTypeReference<Map<String, String>>() {};

//

request.body(BodyExtractors.toMono(BODY_TYPE))
    .switchIfEmpty(Mono.just(Collections.emptyMap()))

Affects: 5.0 RC2

Referenced from: commits 9d04c04, ed5cc27

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 14, 2017

Andy Wilkinson commented

Hi, Rossen. Thanks for improving this on the annotation side of things. Would it be possible to do the same for the functional side too?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 14, 2017

Andy Wilkinson commented

Reopening to consider making the same change on the functional side too

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 14, 2017

Rossen Stoyanchev commented

Good point, fixed.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 14, 2017

Andy Wilkinson commented

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.