为了账号安全,请及时绑定邮箱和手机立即绑定

@ExceptionHandler 不捕获 MethodArgumentNotValid

@ExceptionHandler 不捕获 MethodArgumentNotValid

慕村225694 2021-10-17 16:01:33
我有以下几点@ControllerAdvice:@ControllerAdvicepublic class ExceptionHandlingController {    @ExceptionHandler(value = { MethodArgumentNotValidException.class,            EntityExistsException.class, BadCredentialsException.class, MismatchedInputException.class })    public ResponseEntity<ExceptionResponse> invalidInput(RuntimeException ex) {        ExceptionResponse response = new ExceptionResponse();        response.setErrorCode("BAD_REQUEST");        response.setErrorMessage(ex.getMessage());        return new ResponseEntity<ExceptionResponse>(response,                HttpStatus.BAD_REQUEST);    }}验证器以这种方式绑定到控制器:@RestController@RequestMapping("/api/authentication")public class UserAccountControllerImpl implements UserAccountController {    @Autowired    private UserAccountService userAccountService;    @Override    public UserAccountEntity login(@Valid @RequestBody UserAccountEntity account,            HttpServletResponse response) throws BadCredentialsException {        return userAccountService.authenticateUserAndSetResponsenHeader(                account.getUsername(), account.getPassword(), response);    }    @Override    public UserAccountEntity create(@Valid @RequestBody UserAccountEntity userAccount,            HttpServletResponse response) throws EntityExistsException {        String username = userAccount.getUsername();        String password = userAccount.getPassword();        userAccountService.saveIfNotExists(username, password);        return userAccountService.authenticateUserAndSetResponsenHeader(                username, password, response);    }    //used to bind the validator to the incoming request    @InitBinder    public void binder(WebDataBinder binder) {        binder.addValidators(new UserAccountValidator());    }}为什么抓不到MethodArgumentNotValidException?
查看完整描述

1 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

至少,异常 MethodArgumentNotValidException 不是 RuntimeException,但您在该异常处理程序中使用 RuntimeException 参数。对于测试,将方法参数中的 RuntimeException 更改为 Exception。


查看完整回答
反对 回复 2021-10-17
  • 1 回答
  • 0 关注
  • 921 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信