-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPersonController.java
More file actions
50 lines (39 loc) · 1.51 KB
/
PersonController.java
File metadata and controls
50 lines (39 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io.zipcoder.crudapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController// Annotation
@RequestMapping(value = "/person-controller")
public class PersonController {
@Autowired
private PersonRepository personRepository;
public PersonController(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@RequestMapping(value = "/people", method = RequestMethod.POST)
public Person createPerson(@RequestBody Person p) {
return personRepository.save(p);
}
@RequestMapping(value = "/people/", method = RequestMethod.GET)
public List<Person> getPersonList() {
List<Person> newList = new ArrayList<>();
for (Person p : personRepository.findAll()) {
newList.add(p);
}
return newList;
}
@RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
public Person getPerson(@PathVariable Integer id) {
return personRepository.findOne(id);
}
@RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
public Person updatePerson(@PathVariable Integer id, @RequestBody Person p) {
p = personRepository.findOne(id);
return personRepository.save(p);
}
@RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
public void deletePerson(@PathVariable Integer id) {
personRepository.delete(id);
}
}