diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java index 1fe5e8bab1..3d5e42ab2c 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java @@ -31,7 +31,6 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -67,11 +66,11 @@ public class RegistrationController { @RequestMapping(value = "/user/registration", method = RequestMethod.POST) @ResponseBody - public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) { + public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final HttpServletRequest request) { LOGGER.debug("Registering user account with information: {}", accountDto); - if (result.hasErrors()) { - return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors()); - } + // if (result.hasErrors()) { + // return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors()); + // } final User registered = createUserAccount(accountDto); if (registered == null) { return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale())); diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java index d263e0fc26..5f559bbfc1 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java @@ -7,6 +7,8 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.mail.MailAuthenticationException; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; @@ -24,6 +26,15 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH // API + // 400 + @Override + protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { + logger.error("400 Status Code", ex); + BindingResult result = ex.getBindingResult(); + final GenericResponse bodyOfResponse = new GenericResponse(result.getFieldErrors(), result.getGlobalErrors()); + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + } + // 404 @ExceptionHandler({ UserNotFoundException.class }) public ResponseEntity handleUserNotFound(final RuntimeException ex, final WebRequest request) {