diff --git a/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp index 8b23b68dd7..9b39761516 100644 --- a/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp @@ -63,26 +63,17 @@ function register(){ if(data.message == "success"){ window.location.href = ""; } - else if(data.message == "email"){ - $("#emailError").show().html(data.error); - } - else{ - - var errors = $.parseJSON(data.message); - $.each( errors, function( index,item ){ - $("#"+item.field+"Error").show().html(item.defaultMessage); - }); - errors = $.parseJSON(data.error); - $.each( errors, function( index,item ){ - $("#globalError").show().append(item.defaultMessage+"
"); - }); - } + + }) .fail(function(data) { if(data.responseJSON.error.indexOf("MailError") > -1) { window.location.href = ""; } + else if(data.responseJSON.error == "UserAlreadyExist"){ + $("#emailError").show().html(data.responseJSON.message); + } else if(data.responseJSON.error.indexOf("InternalError") > -1){ window.location.href = "" + "?message=" + data.responseJSON.message; } 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 c0c3f7c02f..10f2c64bc5 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 @@ -14,6 +14,7 @@ import org.baeldung.persistence.service.IUserService; import org.baeldung.persistence.service.UserDto; import org.baeldung.registration.OnRegistrationCompleteEvent; import org.baeldung.validation.EmailExistsException; +import org.baeldung.web.error.UserAlreadyExistException; import org.baeldung.web.error.UserNotFoundException; import org.baeldung.web.util.GenericResponse; import org.slf4j.Logger; @@ -71,7 +72,7 @@ public class RegistrationController { final User registered = createUserAccount(accountDto); if (registered == null) { - return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale())); + throw new UserAlreadyExistException(); } final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl)); 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 ee239fd598..58c089a733 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 @@ -52,6 +52,14 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); } + // 409 + @ExceptionHandler({ UserAlreadyExistException.class }) + public ResponseEntity handleUserAlreadyExist(final RuntimeException ex, final WebRequest request) { + logger.error("409 Status Code", ex); + final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.regError", null, request.getLocale()), "UserAlreadyExist"); + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request); + } + // 500 @ExceptionHandler({ MailAuthenticationException.class }) public ResponseEntity handleMail(final RuntimeException ex, final WebRequest request) { diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/UserAlreadyExistException.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/UserAlreadyExistException.java new file mode 100644 index 0000000000..d9c112c3f5 --- /dev/null +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/UserAlreadyExistException.java @@ -0,0 +1,23 @@ +package org.baeldung.web.error; + +public final class UserAlreadyExistException extends RuntimeException { + + private static final long serialVersionUID = 5861310537366287163L; + + public UserAlreadyExistException() { + super(); + } + + public UserAlreadyExistException(final String message, final Throwable cause) { + super(message, cause); + } + + public UserAlreadyExistException(final String message) { + super(message); + } + + public UserAlreadyExistException(final Throwable cause) { + super(cause); + } + +} diff --git a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp index 6660f9ac52..34f51c2881 100644 --- a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp @@ -6,6 +6,7 @@ + <spring:message code="label.form.title"></spring:message> @@ -16,74 +17,68 @@
-
+
- +
- +
- +
- +
- +

- +

"> -