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