|
@@ -0,0 +1,41 @@
|
|
1
|
+package com.example.throwupthursday;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
5
|
+import org.springframework.stereotype.Controller;
|
|
6
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
7
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
8
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
9
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
10
|
+
|
|
11
|
+import com.example.throwupthursday.daos.Person;
|
|
12
|
+import com.example.throwupthursday.PersonRepository;
|
|
13
|
+
|
|
14
|
+@Controller // This means that this class is a Controller
|
|
15
|
+@RequestMapping(path="/people") // This means URL's start with /demo (after Application path)
|
|
16
|
+public class PersonController {
|
|
17
|
+ @Autowired // This means to get the bean called userRepository
|
|
18
|
+ // Which is auto-generated by Spring, we will use it to handle the data
|
|
19
|
+ private PersonRepository personRepository;
|
|
20
|
+
|
|
21
|
+ @GetMapping(path="/add") // Map ONLY GET Requests
|
|
22
|
+ public @ResponseBody String addNewUser (@RequestParam String name
|
|
23
|
+ , @RequestParam Integer age) {
|
|
24
|
+ // @ResponseBody means the returned String is the response, not a view name
|
|
25
|
+ // @RequestParam means it is a parameter from the GET or POST request
|
|
26
|
+
|
|
27
|
+ Person n = new Person();
|
|
28
|
+ n.setName(name);
|
|
29
|
+ n.setAge(age);
|
|
30
|
+ personRepository.save(n);
|
|
31
|
+ return "Saved";
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ @GetMapping(path="/all")
|
|
35
|
+ public @ResponseBody Iterable<Person> getAllUsers() {
|
|
36
|
+ // This returns a JSON or XML with the users
|
|
37
|
+ return personRepository.findAll();
|
|
38
|
+ }
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+// $ curl 'localhost:8080/people/add?name=George&age=28'
|