David Ginzberg преди 7 години
родител
ревизия
9288c4e428
променени са 1 файла, в които са добавени 48 реда и са изтрити 1 реда
  1. 48
    1
      README.md

+ 48
- 1
README.md Целия файл

@@ -4,7 +4,49 @@ This repository provides a starter project for a CRUD application using Spring B
4 4
 
5 5
 ## Exercise
6 6
 
7
-Using this project as a starter, create a Person class as an entity to persist to H2 with an autogenerated id. Provide a RestController for Person with endpoints for HTTP GET, PUT, POST, and DELETE operations mapping to their appropriate CRUD behaviors. Endpoints should be at `/person` and `/person/{id}` as appropriate and provide the correct HTTP status codes in the response. See the snippet below for setting Person IDs:
7
+Using this project as a starter, you will create a Person class as an entity to persist to H2 with an autogenerated id, and provide access to CRUD operations on those entities using a Spring Rest Controller supporting `GET`, `PUT`, `POST`, and `DELETE` operations. Spring Boot will handle the actual HTTP traffic and database components for you, you just need to build the last few pieces of the puzzle described in this lab.
8
+
9
+As you are working on this lab, you can demo the behavior of your `GET` endpoints using your browser. For `PUT`, `POST`, and `DELETE` you will need a more robust tool, such as [Postman](https://www.getpostman.com/) or [curl](https://curl.haxx.se/).
10
+
11
+### Part 1:
12
+
13
+Create a `Person` class with fields for first name, last name, and an id number.
14
+
15
+Create a `PersonController` class with `Person createPerson(Person p)`, `Person getPerson(int id)`, `List<Person> getPersonList()`, `Person updatePerson(Person p)`, and `void DeletePerson(int id)` methods, and let it track a list of Person objects.
16
+
17
+Add the `@RestController` annotation to your `PersonController` class, and using the "Endpoints" list in the Reference section below, add the appropriate `@RequestMapping` annotations to each of your methods. Endpoints should be at `/people` and `/people/{id}` as appropriate. You will have to use `@PathVariable` for id numbers in the URI and `@RequestBody` for Person objects sent in the requests.
18
+
19
+
20
+### Part 2: 
21
+
22
+Add the `@Entity` and `@Id` annotations to your Person class as shown in the Reference section. These tell Spring how to convert your Person objects to database entities when you pass them to a repository.
23
+
24
+Create a `PersonRepository` interface that extends the `CrudRepository` interface. Be sure to specify the `Person` type parameter on `CrudRepository<>`. You will not need to implement this interface as Spring automatically generates an implementation at runtime.
25
+
26
+Update your controller logic to use the `PersonRepository` instead of manually tracking Person objects in a list. You will need a `PersonRepository` field marked with the `@Autowired` annotation -- again, Spring will provide an implementation here automatically. You will need to use the `findAll()`, `findOne(id)`, `save(Person)` and `delete(id)` methods of `PersonRepository` to fetch and save Person objects.
27
+
28
+### Part 3:
29
+
30
+
31
+Now that your CRUD application is working, it's time to make sure the correct HTTP response codes are sent. Check the list of endpoints in the Reference section for the correct responses and change your `PersonController` methods to produce `ResponseEntity` objects containing the appropriate values.
32
+
33
+
34
+### Reference
35
+
36
+**Endpoints:**
37
+
38
+- `POST /people` - create a new person
39
+  - Response: `201 Created`
40
+- `GET /people` - get the list of all people
41
+  - Response: `200 OK` 
42
+- `GET /people/{id}` - Get the person with id number `{id}`
43
+  - Response: `200 OK` if found, else `404 Not Found`
44
+- `PUT /people/{id}` - Update the person with id number `{id}`
45
+  - Response: `200 OK` if updated, `201 Created` if a new entity was created
46
+- `DELETE /people/{id}` - delete the person with id number `{id}`
47
+  - Response: `204 No Content`
48
+
49
+**Person class and  ID configuration for H2**:
8 50
 
9 51
 ```Java
10 52
 @Entity
@@ -16,3 +58,8 @@ public class Person {
16 58
     ...
17 59
 }
18 60
 ```
61
+
62
+## Resources
63
+
64
+- For more practice building RESTful web services, see the [Spring Boot Guides](https://spring.io/guides)
65
+- For an example of how to properly test an app such as this one, see [this example repo](https://github.com/Zipcoder/SpringBootWithUnitTest)