Bläddra i källkod

made some changes in my controller

Roy 6 år sedan
förälder
incheckning
2c788f0e35
1 ändrade filer med 7 tillägg och 5 borttagningar
  1. 7
    5
      server/src/main/java/com/example/demo/user/UserController.java

+ 7
- 5
server/src/main/java/com/example/demo/user/UserController.java Visa fil

1
 package com.example.demo.user;
1
 package com.example.demo.user;
2
 
2
 
3
+import com.fasterxml.jackson.databind.ObjectMapper;
3
 import org.springframework.beans.factory.annotation.Autowired;
4
 import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.http.ResponseEntity;
4
 import org.springframework.stereotype.Controller;
6
 import org.springframework.stereotype.Controller;
5
 import org.springframework.web.bind.annotation.*;
7
 import org.springframework.web.bind.annotation.*;
6
 
8
 
11
 
13
 
12
     @Autowired // this means to get bean called userRepository which is auto-generated by spring. it is ued to handle data
14
     @Autowired // this means to get bean called userRepository which is auto-generated by spring. it is ued to handle data
13
     private UserRepository userRepository;
15
     private UserRepository userRepository;
14
-
16
+        ObjectMapper om = new ObjectMapper();
15
 
17
 
16
     @PostMapping(path="/add")
18
     @PostMapping(path="/add")
17
-    public @ResponseBody String addNewUser(@RequestParam String username, @RequestParam String email, @RequestParam String password){ //ResponseBody means the returned string is the response
19
+    //public @ResponseBody String addNewUser(@RequestParam String username, @RequestParam String email, @RequestParam String password){ //ResponseBody means the returned string is the response
18
         //RequestParam means it is a parameter from a GET or POST request
20
         //RequestParam means it is a parameter from a GET or POST request
19
-        User n = new User(username, email, password);
20
-        userRepository.save(n);
21
-        return "Saved";
21
+      public @ResponseBody String addNewuser(@RequestBody User user) {
22
+        userRepository.save(user);
23
+        return "saved";
22
     }
24
     }
23
 
25
 
24
     @GetMapping(path="/all")
26
     @GetMapping(path="/all")