我有一个带有发布请求的控制器。我正在尝试使用简单的 NotNull 注释来验证 POJO。我正在使用 ControllerAdvice 来处理异常。
@PostMapping("/something")
public MyResponse post(@Valid MyRequest request) {
// nevermind...
}
public class MyRequest {
@NotNull
private Integer something;
// Getters setters nevermind...
}
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = BindException.class)
protected ResponseEntity<Object> handleBindException(RuntimeException ex, WebRequest request) {
return handleExceptionInternal(...);
}
}
所以我正在尝试使用它,但是当我启动应用程序时,我得到了以下信息:
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception; nested exception is java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.validation.BindException]: {protected org.springframework.http.ResponseEntity com.liligo.sponsoredads.controller.RestResponseEntityExceptionHandler.handleBindException(java.lang.Exception,org.springframework.web.context.request.WebRequest), public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest) throws java.lang.Exception}
所以我想为 BindExceptions 创建自己的处理程序,但是当我为 BindException 类创建 ExceptionHandler 时,spring 应用程序没有启动。如果我注释掉handleBindException 方法,应用程序将启动,如果发生BindException,它只会返回400 并注销错误,但没有任何内容作为响应正文发回。
为 BindExceptions 创建自定义处理程序的解决方案是什么?
江户川乱折腾
相关分类