小编asv*_*vni的帖子

我可以获得 Spring Boot Web 应用程序中的所有验证错误吗?

我有一个如下所示的 pojo(请假设一个控制器和其余代码。应用程序位于 Spring boot 中):

@Getter  @Setter
@AllArgsConstructor  @NoArgsConstructor
public class User  {
    @NotBlank(message = "userName is blank")
    private String userName;

    @NotBlank(message = "secretKey is blank")
    private String secretKey;
}
Run Code Online (Sandbox Code Playgroud)

并定义了一个 ExceptionHandler 类,@ControllerAdvice并定义了一个方法,如下所示:

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    protected ResponseEntity<ErrorResponse> handleMethodArgNotValidException(MethodArgumentNotValidException ex,Locale locale) {
        // code to handle exception.
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {WebExchangeBindException.class})
    protected ResponseEntity<ErrorResponse> handleException(WebExchangeBindException ex, Locale locale) {
        // code to handle exception.
    }
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,即使两个字段都有验证错误,客户端也只能得到一个。

我想问有什么方法可以列出该端点响应中的所有验证错误吗?

curl --location --request POST 'localhost/api/login' \
--header 'Content-Type: application/json' \ …
Run Code Online (Sandbox Code Playgroud)

java validation exceptionhandler spring-boot

2
推荐指数
1
解决办法
3388
查看次数

标签 统计

exceptionhandler ×1

java ×1

spring-boot ×1

validation ×1