Browse Source

post method

Trinh Tong 7 years ago
parent
commit
3e271d4865

+ 10
- 6
src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java View File

@@ -6,26 +6,30 @@ import org.springframework.beans.factory.annotation.Autowired;
6 6
 import org.springframework.http.HttpStatus;
7 7
 import org.springframework.http.ResponseEntity;
8 8
 
9
-import org.springframework.web.bind.annotation.PathVariable;
10
-import org.springframework.web.bind.annotation.RequestMapping;
11
-import org.springframework.web.bind.annotation.RequestMethod;
12
-import org.springframework.web.bind.annotation.RestController;
9
+import org.springframework.web.bind.annotation.*;
10
+
11
+import javax.persistence.criteria.CriteriaBuilder;
13 12
 
14 13
 @RestController
15 14
 public class PersonController {
16 15
     @Autowired
17 16
     PersonService personService;
18 17
 
19
-    @RequestMapping(value="/person", method= RequestMethod.GET)
18
+    @RequestMapping(value="/people", method= RequestMethod.GET)
20 19
     public ResponseEntity<Iterable<Person>> getPeople() {
21 20
         Iterable<Person> people = personService.getAllPeople();
22 21
         return new ResponseEntity<>(people, HttpStatus.OK);
23 22
     }
24 23
 
25
-    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
24
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
26 25
     public ResponseEntity<Person> getPersonById(@PathVariable Integer id) {
27 26
         Person p = personService.findPersonById(id);
28 27
         return new ResponseEntity<>(p, HttpStatus.OK);
29 28
     }
30 29
 
30
+    @PostMapping("/people")
31
+    public ResponseEntity<Person> createPerson(@RequestBody Person p) {
32
+        personService.add(p);
33
+        return new ResponseEntity<>(p, HttpStatus.CREATED);
34
+    }
31 35
 }

+ 2
- 2
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java View File

@@ -43,8 +43,8 @@ public class PersonService {
43 43
     public void add(Person p) {
44 44
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
45 45
         jdbcTemplate.update(
46
-                "INSERT INTO person (first_name, last_name, mobile, birthday, home_ID)" +
47
-                        "VALUES (?, ?, ?, ?, ?)", info, types);
46
+                "INSERT INTO person(first_name, last_name, mobile, birthday, home_ID)" +
47
+                        "VALUES (?, ?, ?, ?, ?)", info);
48 48
     }
49 49
 
50 50
     public void update(Person p) {