11package com .baeldung .springmvcforms .controller ;
22
3- import java .util .ArrayList ;
4- import java .util .Collections ;
5- import java .util .List ;
6-
7- import javax .validation .Valid ;
3+ import com .baeldung .springmvcforms .domain .User ;
4+ import org .springframework .context .support .DefaultMessageSourceResolvable ;
85import org .springframework .http .HttpStatus ;
9- import org .springframework .http .MediaType ;
106import org .springframework .http .ResponseEntity ;
117import org .springframework .stereotype .Controller ;
128import org .springframework .ui .Model ;
139import org .springframework .validation .BindingResult ;
14- import org .springframework .validation .ObjectError ;
1510import org .springframework .web .bind .annotation .GetMapping ;
1611import org .springframework .web .bind .annotation .PostMapping ;
1712import org .springframework .web .bind .annotation .ResponseBody ;
1813
19- import com .baeldung .springmvcforms .domain .User ;
14+ import javax .validation .Valid ;
15+ import java .util .Arrays ;
16+ import java .util .Collections ;
17+ import java .util .List ;
18+ import java .util .stream .Collectors ;
2019
2120@ Controller
2221public class UserController {
2322
24- List <User > users = new ArrayList <User >() {
25- {
26- add (new User ("ana@yahoo.com" , "pass" , "Ana" , 20 ));
27- add (new User ("bob@yahoo.com" , "pass" , "Bob" , 30 ));
28- add (new User ("john@yahoo.com" , "pass" , "John" , 40 ));
29- add (new User ("mary@yahoo.com" , "pass" , "Mary" , 30 ));
30- }
31- };
23+ private List <User > users = Arrays .asList (
24+ new User ("ana@yahoo.com" , "pass" , "Ana" , 20 ),
25+ new User ("bob@yahoo.com" , "pass" , "Bob" , 30 ),
26+ new User ("john@yahoo.com" , "pass" , "John" , 40 ),
27+ new User ("mary@yahoo.com" , "pass" , "Mary" , 30 ));
3228
3329 @ GetMapping ("/userPage" )
3430 public String getUserProfilePage () {
3531 return "user" ;
3632 }
3733
38- @ PostMapping (value = "/user" , produces = MediaType . APPLICATION_JSON_VALUE )
34+ @ PostMapping ("/user" )
3935 @ ResponseBody
4036 public ResponseEntity <Object > saveUser (@ Valid User user , BindingResult result , Model model ) {
4137 if (result .hasErrors ()) {
42- List <String > errors = new ArrayList <>();
43- result . getAllErrors (). forEach ( item ->{
44- errors . add ( item . getDefaultMessage ());
45- });
38+ final List <String > errors = result . getAllErrors (). stream ()
39+ . map ( DefaultMessageSourceResolvable :: getDefaultMessage )
40+ . collect ( Collectors . toList ());
41+
4642 return new ResponseEntity <>(errors , HttpStatus .OK );
4743 } else {
4844 if (users .stream ().anyMatch (it -> user .getEmail ().equals (it .getEmail ()))) {
@@ -54,7 +50,7 @@ public ResponseEntity<Object> saveUser(@Valid User user, BindingResult result, M
5450 }
5551 }
5652
57- @ GetMapping (value = "/users" , produces = MediaType . APPLICATION_JSON_VALUE )
53+ @ GetMapping ("/users" )
5854 @ ResponseBody
5955 public List <User > getUsers () {
6056 return users ;
0 commit comments