bean 验证工作但 Kotlin 上的消息为空

bean validation working but empty message on Kotlin

我正在尝试让 Kotlin 在 spring-webflux 项目上使用 bean 验证。
请求似乎已正确验证,但响应主体的消息为空,因此很难知道错误原因。

很想从响应中获取默认验证消息?

控制器:

class SomeController {
    @PostMapping("/foo")
    fun foo(@Valid @RequestBody body: FooRequest): Mono<FooRequest> {
        return Mono.just(body)
    }
}

要求:

data class FooRequest(
    @field:Min(0)
    val bar: Int
)

用请求"{\"bar\":-1}"调用那个api的响应是

{
  "timestamp": "2021-03-23T02:18:49.368+00:00",
  "path": "/api/v1/foo",
  "status": 400,
  "error": "Bad Request",
  "message": "",
  "requestId": "d1739c79-6"
}

所以读完这个https://www.baeldung.com/spring-boot-bean-validation

我最终添加了这样的异常处理程序:

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Map<String,String>> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) {
    Map<String, String> errors = new HashMap<>();
    exception.getBindingResult().getAllErrors().forEach((error) -> {
        String fieldName = ((FieldError) error).getField();
        String errorMessage = error.getDefaultMessage();
        errors.put(fieldName, errorMessage);
    });
    return ResponseEntity
            .status(HttpStatus.BAD_REQUEST)
            .body(errors);
}