支持处理程序异常

时间:2017-07-28 03:24:29

标签: java spring rest spring-boot

我有一个控制器

@PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<ChangeEmailDTO> showChangeEMail(
        @RequestBody @Valid ChangeEmailDTO changeEmailDTO
) {
    System.out.println("Email: " + changeEmailDTO.getEmail());

    return ResponseEntity.ok(changeEmailDTO);
}

验证失败时抛出异常MethodArgumentNotValidException。我创建了这个异常服务

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationErrorDTO processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    return processFieldErrors(fieldErrors);
}

仍在控制台中抛出异常

org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: public org.springframework.http.HttpEntity<com.movie.database.app.dto.ChangeEmailDTO> com.movie.database.app.rest.controller.UserController.showChangeEMail(com.movie.database.app.dto.ChangeEmailDTO), with 2 error(s): [Field error in object 'changeEmailDTO' on field 'email': rejected value []; codes [IsValidEmail.changeEmailDTO.email,IsValidEmail.email,IsValidEmail.java.lang.String,IsValidEmail]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [changeEmailDTO.email,email]; arguments []; default message [email]]; default message [Please enter a valid e-mail address.]] [Field error in object 'changeEmailDTO' on field 'email': rejected value []; codes [NotEmpty.changeEmailDTO.email,NotEmpty.email,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [changeEmailDTO.email,email]; arguments []; default message [email]]; default message [may not be empty]]

如果我创建了他的服务,为什么要在控制台中抛出异常?

0 个答案:

没有答案