同一类异常,只能有一个方法处理,否则无法映射
java.lang.IllegalStateException:
Ambiguous
@ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]:
{public com.book.springtest.exception.AjaxResponse com.book.springtest.exception.WebExceptionHandler.
handleILLegalException(java.lang.IllegalArgumentException),
public com.book.springtest.exception.AjaxResponse com.book.springtest.exception.WebExceptionHandler.
handleValidException(org.springframework.web.bind.MethodArgumentNotValidException)}
处理IllegalArgumentException异常。
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public AjaxResponse handleILLegalException(IllegalArgumentException e) {
return AjaxResponse.error(
CommonExceptionType.USER_INPUT_ERROR,
e.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public AjaxResponse handleValidException(
MethodArgumentNotValidException e) {
FieldError fieldError = e.getBindingResult().getFieldError();
return AjaxResponse.error(
CommonExceptionType.USER_INPUT_ERROR,
fieldError.getDefaultMessage());
}
文章讨论了在SpringMVC应用中遇到的一个问题,即定义了两个@ExceptionHandler来分别处理MethodArgumentNotValidException和IllegalArgumentException,导致出现IllegalStateException,因为系统无法确定哪个方法应被用于处理特定异常。解决方案通常涉及删除或调整重复的异常处理方法。
1086

被折叠的 条评论
为什么被折叠?



