Java 如何在SpringBoot REST Api中调用重写的handleMethodArgumentNotValid?

Java 如何在SpringBoot REST Api中调用重写的handleMethodArgumentNotValid?,java,spring,spring-boot,Java,Spring,Spring Boot,我正在努力在spring boot REST api中调用重写的MethodArgumentNotValidException。我的其他异常处理程序工作起来很有魅力,但是永远不会触发重写标准handleMethodArgumentNotValid 有人知道我错过了什么吗 波乔 控制器方法: @RestController("fundsConfirmationController") @RequestMapping( value="/accounts/{accountId}/fun

我正在努力在spring boot REST api中调用重写的MethodArgumentNotValidException。我的其他异常处理程序工作起来很有魅力,但是永远不会触发重写标准handleMethodArgumentNotValid

有人知道我错过了什么吗

波乔

控制器方法:

@RestController("fundsConfirmationController")
@RequestMapping(
        value="/accounts/{accountId}/funds-confirmations"
)
public class FundsConfirmationController implements FundsConfirmationControllerI{
@GetMapping(
            headers = {"X-CAF-MSGID", "X-AccessToken"},
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public ResponseEntity<?> fundsConfirmation(@RequestHeader(value="X-CAF-MSGID") String messageId,
                                               @RequestHeader(value="X-AccessToken") String accessToken,
                                               @Valid FundsConfirmationRequest requestParams) throws FIClientException, FIParseException {
通过@RestControllerAdvice执行异常处理程序

@RestControllerAdvice
    public class FundsConfirmationExceptionHandler extends ResponseEntityExceptionHandler {

//Existing Exception Handlers
@Override
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    System.out.println("Custom handleMethodArgumentNotValid method");
    FundsConfirmationError responseBody = new FundsConfirmationError(HttpStatus.BAD_REQUEST.toString(), "Input Validation Failed. Parameter.: " + ex.getParameter().getParameterName() + " Value.: " + ex.getParameter().getParameter().toString() + " " + ex.getMessage(), Severity.ERROR.toString(), Sources.LOCAL_CAF_API.toString() );
    return ResponseEntity
            .status(HttpStatus.BAD_REQUEST)
            .header("X-CAF-ResponseID", request.getHeader("X-CAF-MSGID"))
            .body(responseBody);
}

显然,这是因为春天的“魔力”。这涉及到我不太熟悉的各种概念,因为框架“隐藏”了这种复杂性

在我的示例中,我有一个“GET”请求,我将其pathParams/requestParams映射到一个复杂对象。作为额外的,我想对这些参数进行验证

然而,由于“数据绑定到复杂对象”在Spring中的工作方式,不需要任何注释。因此,这是“数据绑定”,而不是“方法映射”。此特定案例触发的结果异常不是MethodArgumentNotValid,而是BindException


Spring如何准确地将数据映射到REST调用中的对象取决于各种情况,如ContentType、使用的注释等等

显然,这是因为春天的“魔力”。这涉及到我不太熟悉的各种概念,因为框架“隐藏”了这种复杂性

在我的示例中,我有一个“GET”请求,我将其pathParams/requestParams映射到一个复杂对象。作为额外的,我想对这些参数进行验证

然而,由于“数据绑定到复杂对象”在Spring中的工作方式,不需要任何注释。因此,这是“数据绑定”,而不是“方法映射”。此特定案例触发的结果异常不是MethodArgumentNotValid,而是BindException

Spring如何准确地将数据映射到REST调用中的对象取决于各种情况,如ContentType、使用的注释等等

我认为您需要添加@OrderOrdered.HIGHEST\u优先级注释和@ControllerAdvice

@RestControllerAdvice
    public class FundsConfirmationExceptionHandler extends ResponseEntityExceptionHandler {

//Existing Exception Handlers
@Override
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    System.out.println("Custom handleMethodArgumentNotValid method");
    FundsConfirmationError responseBody = new FundsConfirmationError(HttpStatus.BAD_REQUEST.toString(), "Input Validation Failed. Parameter.: " + ex.getParameter().getParameterName() + " Value.: " + ex.getParameter().getParameter().toString() + " " + ex.getMessage(), Severity.ERROR.toString(), Sources.LOCAL_CAF_API.toString() );
    return ResponseEntity
            .status(HttpStatus.BAD_REQUEST)
            .header("X-CAF-ResponseID", request.getHeader("X-CAF-MSGID"))
            .body(responseBody);
}
我认为您需要添加@OrderOrdered.HIGHEST\u优先级注释和@ControllerAdvice

@RestControllerAdvice
    public class FundsConfirmationExceptionHandler extends ResponseEntityExceptionHandler {

//Existing Exception Handlers
@Override
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    System.out.println("Custom handleMethodArgumentNotValid method");
    FundsConfirmationError responseBody = new FundsConfirmationError(HttpStatus.BAD_REQUEST.toString(), "Input Validation Failed. Parameter.: " + ex.getParameter().getParameterName() + " Value.: " + ex.getParameter().getParameter().toString() + " " + ex.getMessage(), Severity.ERROR.toString(), Sources.LOCAL_CAF_API.toString() );
    return ResponseEntity
            .status(HttpStatus.BAD_REQUEST)
            .header("X-CAF-ResponseID", request.getHeader("X-CAF-MSGID"))
            .body(responseBody);
}

在POJO和请求上显示验证部分body@Deadpool:添加了请求的部件。我使用了诸如getter/setter之类的通用工具来限制代码剪切。对于GET请求,您不能有body并将请求更改为posit's not body。它的请求参数映射到FundsConfigurationRequestPOJO。即,让我们在POJO和请求上显示验证部分body@Deadpool:添加了请求的部件。我使用了诸如getter/setter之类的通用工具来限制代码剪切。对于GET请求,您不能有body并将请求更改为posit's not body。它的请求参数映射到FundsConfigurationRequestPOJO。也就是说,让我们。