Spring mvc 重构类似的Spring MVC异常处理程序

Spring mvc 重构类似的Spring MVC异常处理程序,spring-mvc,spring-java-config,Spring Mvc,Spring Java Config,我在两个SpringMVC控制器中重复了以下异常处理程序。我知道这些可以重构成单个bean或WebMVCConfigureAdapter,但我找不到任何示例。我没有任何视图,这是一个RESTAPI 我如何用java配置重构它们?如果可能的话,我更愿意将它们重构成自定义的WebMVCConfigureAdapter ...... ...... @ExceptionHandler({MissingServletRequestParameterException.class, Unsatisf

我在两个SpringMVC控制器中重复了以下异常处理程序。我知道这些可以重构成单个bean或WebMVCConfigureAdapter,但我找不到任何示例。我没有任何视图,这是一个RESTAPI

我如何用java配置重构它们?如果可能的话,我更愿意将它们重构成自定义的WebMVCConfigureAdapter

......
......
@ExceptionHandler({MissingServletRequestParameterException.class,
    UnsatisfiedServletRequestParameterException.class,
    HttpRequestMethodNotSupportedException.class,
    ServletRequestBindingException.class,
    TypeMismatchException.class
})
public ResponseEntity<DataCarrier> handleException(Exception exception) {
    logger.warn("A bad input received.");
    return new ResponseEntity<DataCarrier>(new ErrorDto("Input data is not valid."), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler({AccountNotFoundException.class, NoAccountException.class})
public ResponseEntity<DataCarrier> noAccountFoundException(Exception exception) {
    return new ResponseEntity<DataCarrier>(HttpStatus.UNAUTHORIZED);
}

@ExceptionHandler({HeaderPropertyException.class})
public ResponseEntity<DataCarrier> headerPropertyException(Exception exception) {
    logger.error("[{}].", exception.getMessage(), exception);
    return new ResponseEntity<DataCarrier>(HttpStatus.INTERNAL_SERVER_ERROR);
}

那你怎么知道可能呢?直觉更好的解决方案是添加一个Ok,我通过添加一个ControllerAdvice解决了这个问题,在一个地方重构了所有分散的异常处理程序。