Hi
Using Spring-Boot v2.4.5 with starter-webflux
I have a @RestController class with the endpoint:
@PostMapping("/register")
public Mono<ResponseEntity<ScheduledTask>> registerScheduler(@RequestBody @Valid NewScheduledTaskRequest task) {
****
}
The NewScheduledTaskRequest
class has few fields annotated with validations like:
@NotBlank(message = "serviceName is mandatory field")
private String serviceName;
@NotBlank(message = "taskName is mandatory field")
private String taskName;
When I send a broken request on purpose with the empty field (any of them) I'm getting in response:
{
"timestamp": "2021-05-18T14:13:17.334+00:00",
"path": "/register",
"status": 400,
"error": "Bad Request",
"message": "",
"requestId": "d9da178b-2"
}
Where message
field is empty, the Validation message was not set or propagated. I'm pretty sure it was working before, but not anymore
As a temp solution, I annotated the controller class with @RestControllerAdvice
And added handler method:
@ExceptionHandler(WebExchangeBindException.class)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public Map<String, Object> validationExceptionHandling(ServerHttpRequest request, WebExchangeBindException e) {
Map<String, Object> errorResponse = new HashMap<>();
errorResponse.put("timestamp", LocalDateTime.now());
errorResponse.put("path", request.getPath().value());
errorResponse.put("status", HttpStatus.BAD_REQUEST.value());
errorResponse.put("error", "Bad Request");
errorResponse.put("message", e.getAllErrors().get(0).getDefaultMessage());
errorResponse.put("requestId", request.getId());
return errorResponse;
}
Though it is a very manual and unnecessary step, the original message should be propagated automatically.
Comment From: wilkinsona
This is the expected default behaviour in Spring Boot 2.3 and later. Please see the relevant section of the release notes for details.
Duplicates #22049.