From 3accbf88156f1aa4850dd29c7dae8a9b42e0f255 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 19 Mar 2017 22:31:11 +0100 Subject: [PATCH] UserController refactor (#1450) * Refactor UserController * Refactor UserController --- .../controller/UserController.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java index 880b224dd9..f5553cf2b7 100644 --- a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java @@ -1,48 +1,44 @@ package com.baeldung.springmvcforms.controller; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import javax.validation.Valid; +import com.baeldung.springmvcforms.domain.User; +import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; -import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; -import com.baeldung.springmvcforms.domain.User; +import javax.validation.Valid; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; @Controller public class UserController { - List users = new ArrayList() { - { - add(new User("ana@yahoo.com", "pass", "Ana", 20)); - add(new User("bob@yahoo.com", "pass", "Bob", 30)); - add(new User("john@yahoo.com", "pass", "John", 40)); - add(new User("mary@yahoo.com", "pass", "Mary", 30)); - } - }; + private List users = Arrays.asList( + new User("ana@yahoo.com", "pass", "Ana", 20), + new User("bob@yahoo.com", "pass", "Bob", 30), + new User("john@yahoo.com", "pass", "John", 40), + new User("mary@yahoo.com", "pass", "Mary", 30)); @GetMapping("/userPage") public String getUserProfilePage() { return "user"; } - @PostMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) + @PostMapping("/user") @ResponseBody public ResponseEntity saveUser(@Valid User user, BindingResult result, Model model) { if (result.hasErrors()) { - List errors = new ArrayList<>(); - result.getAllErrors().forEach(item->{ - errors.add(item.getDefaultMessage()); - }); + final List errors = result.getAllErrors().stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .collect(Collectors.toList()); + return new ResponseEntity<>(errors, HttpStatus.OK); } else { if (users.stream().anyMatch(it -> user.getEmail().equals(it.getEmail()))) { @@ -54,7 +50,7 @@ public class UserController { } } - @GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping("/users") @ResponseBody public List getUsers() { return users;