Sfoglia il codice sorgente

modified it to use rest

Nhu Nguyen 8 anni fa
parent
commit
d96fd2847f

+ 6
- 0
src/main/java/com/example/demo/user/User.java Vedi File

@@ -16,6 +16,12 @@ public class User {
16 16
 
17 17
     public User(){}
18 18
 
19
+    public User(long id, String name, boolean active) {
20
+        this.id = id;
21
+        this.name = name;
22
+        this.active = active;
23
+    }
24
+
19 25
     public User(String name) {
20 26
         this.name = name;
21 27
     }

+ 18
- 14
src/main/java/com/example/demo/user/UserController.java Vedi File

@@ -13,34 +13,38 @@ import java.util.Map;
13 13
 import java.util.stream.Collector;
14 14
 import java.util.stream.Collectors;
15 15
 
16
-@Controller
16
+//change this to RestController
17
+@RestController
17 18
 @RequestMapping(path="/users")
18 19
 public class UserController {
20
+
19 21
     @Autowired
20 22
     private UserRepository repository;
21 23
 
22
-    @GetMapping(path="/new")
23
-    public String newUser(Model model){
24
-        model.addAttribute("user", new User());
25
-        return "users/new";
26
-    }
24
+    //Don't need this anymore
25
+//    @GetMapping(path="/new")
26
+//    public String newUser(Model model){
27
+//        model.addAttribute("user", new User());
28
+//        return "users/new";
29
+//    }
27 30
 
31
+    // change @ModelAttribute to @RequestBody
28 32
     @PostMapping
29
-    public String create(@ModelAttribute User user){
33
+    public String create(@RequestBody User user){
30 34
         repository.save(user);
31
-        return "redirect:users";
35
+        return "Created";
32 36
     }
33 37
 
38
+    //change this to return user instead of template name
34 39
     @RequestMapping(value="/{id}")
35
-    public String get(@PathVariable long id, Model model){
36
-        model.addAttribute("user", repository.findById(id));
37
-        return "users/show";
40
+    public User get(@PathVariable long id){
41
+        return repository.findById(id);
38 42
     }
39 43
 
44
+//    change this to return list instead of template name
40 45
     @GetMapping
41
-    public String index(Model model){
42
-        model.addAttribute("allUsers", repository.findAll());
43
-        return "users/index";
46
+    public List<User> index(){
47
+        return repository.findAll();
44 48
     }
45 49
 
46 50