From 1b9a120e2e3cfa39dad9583655639897c218b8b6 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 9 Mar 2015 21:33:28 +0200 Subject: [PATCH] Modify registration controller --- .../controller/RegistrationController.java | 31 +++++-------------- .../baeldung/web/util/GenericResponse.java | 20 ++++++++++++ 2 files changed, 28 insertions(+), 23 deletions(-) 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 ee35f91b56..1fe5e8bab1 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 @@ -32,12 +32,10 @@ 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.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.servlet.ModelAndView; @Controller public class RegistrationController { @@ -67,34 +65,21 @@ public class RegistrationController { // Registration - @RequestMapping(value = "/user/registration", method = RequestMethod.GET) - public String showRegistrationPage(final Model model) { - LOGGER.debug("Rendering registration page."); - final UserDto accountDto = new UserDto(); - model.addAttribute("user", accountDto); - return "registration"; - } - @RequestMapping(value = "/user/registration", method = RequestMethod.POST) - public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) { + @ResponseBody + public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) { LOGGER.debug("Registering user account with information: {}", accountDto); if (result.hasErrors()) { - return new ModelAndView("registration", "user", accountDto); + return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors()); } - final User registered = createUserAccount(accountDto); if (registered == null) { - result.rejectValue("email", "message.regError"); - return new ModelAndView("registration", "user", accountDto); + return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale())); } - try { - final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); - eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl)); - } catch (final Exception ex) { - LOGGER.warn("Unable to register user", ex); - return new ModelAndView("emailError", "user", accountDto); - } - return new ModelAndView("successRegister", "user", accountDto); + final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); + eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl)); + + return new GenericResponse("success"); } @RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET) diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/util/GenericResponse.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/util/GenericResponse.java index b80c8ef578..384bb51589 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/web/util/GenericResponse.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/util/GenericResponse.java @@ -1,5 +1,13 @@ package org.baeldung.web.util; +import java.util.List; + +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + public class GenericResponse { private String message; private String error; @@ -15,6 +23,18 @@ public class GenericResponse { this.error = error; } + public GenericResponse(final List fieldErrors, final List globalErrors) { + super(); + final ObjectMapper mapper = new ObjectMapper(); + try { + this.message = mapper.writeValueAsString(fieldErrors); + this.error = mapper.writeValueAsString(globalErrors); + } catch (final JsonProcessingException e) { + this.message = ""; + this.error = ""; + } + } + public String getMessage() { return message; }