Skip to content

Commit 3accbf8

Browse files
authored
UserController refactor (eugenp#1450)
* Refactor UserController * Refactor UserController
1 parent 0b78cc9 commit 3accbf8

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

‎spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java‎

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
package 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;
85
import org.springframework.http.HttpStatus;
9-
import org.springframework.http.MediaType;
106
import org.springframework.http.ResponseEntity;
117
import org.springframework.stereotype.Controller;
128
import org.springframework.ui.Model;
139
import org.springframework.validation.BindingResult;
14-
import org.springframework.validation.ObjectError;
1510
import org.springframework.web.bind.annotation.GetMapping;
1611
import org.springframework.web.bind.annotation.PostMapping;
1712
import 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
2221
public 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

Comments
 (0)