Skip to content

Commit 1b9a120

Browse files
committed
Modify registration controller
1 parent abd9f11 commit 1b9a120

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@
3232
import org.springframework.stereotype.Controller;
3333
import org.springframework.ui.Model;
3434
import org.springframework.validation.BindingResult;
35-
import org.springframework.web.bind.annotation.ModelAttribute;
3635
import org.springframework.web.bind.annotation.RequestMapping;
3736
import org.springframework.web.bind.annotation.RequestMethod;
3837
import org.springframework.web.bind.annotation.RequestParam;
3938
import org.springframework.web.bind.annotation.ResponseBody;
40-
import org.springframework.web.servlet.ModelAndView;
4139

4240
@Controller
4341
public class RegistrationController {
@@ -67,34 +65,21 @@ public RegistrationController() {
6765

6866
// Registration
6967

70-
@RequestMapping(value = "/user/registration", method = RequestMethod.GET)
71-
public String showRegistrationPage(final Model model) {
72-
LOGGER.debug("Rendering registration page.");
73-
final UserDto accountDto = new UserDto();
74-
model.addAttribute("user", accountDto);
75-
return "registration";
76-
}
77-
7868
@RequestMapping(value = "/user/registration", method = RequestMethod.POST)
79-
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) {
69+
@ResponseBody
70+
public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) {
8071
LOGGER.debug("Registering user account with information: {}", accountDto);
8172
if (result.hasErrors()) {
82-
return new ModelAndView("registration", "user", accountDto);
73+
return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
8374
}
84-
8575
final User registered = createUserAccount(accountDto);
8676
if (registered == null) {
87-
result.rejectValue("email", "message.regError");
88-
return new ModelAndView("registration", "user", accountDto);
77+
return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
8978
}
90-
try {
91-
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
92-
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
93-
} catch (final Exception ex) {
94-
LOGGER.warn("Unable to register user", ex);
95-
return new ModelAndView("emailError", "user", accountDto);
96-
}
97-
return new ModelAndView("successRegister", "user", accountDto);
79+
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
80+
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
81+
82+
return new GenericResponse("success");
9883
}
9984

10085
@RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET)

spring-security-login-and-registration/src/main/java/org/baeldung/web/util/GenericResponse.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package org.baeldung.web.util;
22

3+
import java.util.List;
4+
5+
import org.springframework.validation.FieldError;
6+
import org.springframework.validation.ObjectError;
7+
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
311
public class GenericResponse {
412
private String message;
513
private String error;
@@ -15,6 +23,18 @@ public GenericResponse(String message, String error) {
1523
this.error = error;
1624
}
1725

26+
public GenericResponse(final List<FieldError> fieldErrors, final List<ObjectError> globalErrors) {
27+
super();
28+
final ObjectMapper mapper = new ObjectMapper();
29+
try {
30+
this.message = mapper.writeValueAsString(fieldErrors);
31+
this.error = mapper.writeValueAsString(globalErrors);
32+
} catch (final JsonProcessingException e) {
33+
this.message = "";
34+
this.error = "";
35+
}
36+
}
37+
1838
public String getMessage() {
1939
return message;
2040
}

0 commit comments

Comments
 (0)