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

Custom web input exceptions in WebFlux [SPR-17232] #21765

Closed
spring-issuemaster opened this Issue Aug 31, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Aug 31, 2018

Enzo Bonggio opened SPR-17232 and commented

  1. Having a PostMapping like this
@PostMapping("/body") public Mono<Void> body(@Valid @RequestBody BodyClass bodyClass) { return Mono.empty().then(); }

 

  1. When we send a request with a null body
@Autowired
private TestRestTemplate restTemplate 
def "test post body null"() {
    given:
    def bodyClass = null
    def headers = new HttpHeaders()
    headers.setContentType(MediaType.APPLICATION_JSON);
    def request = new HttpEntity<MultiValueMap<String, String>>(bodyClass, headers);
    when:
    def response = restTemplate.postForEntity("/body", request, ErrorResponse.class)
    then:
    assert response.statusCode == HttpStatus.BAD_REQUEST
}

 

  1. We need to handle ServerWebInputException that is thrown from AbstractMessageReaderArgumentResolver 
private ServerWebInputException handleMissingBody(MethodParameter param) {
     return new ServerWebInputException("Request body is missing: " + param.getExecutable().toGenericString());
}

 

Right now there is no way to override method that is thrown from AbstractMessageReaderArgumentResolver 

At least we should send MethodParameter in the constructor of ServerWebInputException or tell a way to override that implementation   


Affects: 5.0.8

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Aug 31, 2018

Enzo Bonggio commented

I have another case where this happen:

 

When you are missing a header we have the same issue on RequestHeaderMethodArgumentResolver

@Override
protected void handleMissingValue(String name, MethodParameter parameter) {
 String type = parameter.getNestedParameterType().getSimpleName();
 throw new ServerWebInputException("Missing request header '" + name +
 "' for method parameter of type " + type);
}
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 21, 2018

Rossen Stoyanchev commented

Yes, indeed we should be using the constructor with a MethodParameter in places where it is available. 

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