Completing this lab demonstrates your ability to code Spring REST controllers in a standards based way.
Write a fully functional REST controller for the customers API
- Copy the starter code from here into a new, private repository in your personal GitHub account using these instructions substituting this repository URL
https://github.com/jeff-anderson-cscc/java4-labs-rest-controllers
for the one referenced in that document - Create a new branch for your code changes as described in these instructions
Important You may not change the code in any test cases. The only permissible difference between the base version of the JUnit test class and yours is yours will have no tests commented out and the file is otherwise identical.
- Open CustomerControllerTests
- One by one, uncomment each test, changing just enough code or configuration to make the test pass
- Once all tests are passing, commit your changes
Each test validates conditions detailed in the specifications below
Class Diagram:
- You will be creating a CustomerController class which is shown in the class diagram above
- Your class should have the same method names and signatures
- The CustomerRepository interface is provided. Do not implement a concrete class for this interface or modify it's contents.
- The Customer class is provided. Do not modify it's contents.
- Example Request:
GET localhost:8080/api/customers
- Step 1.1.3 puts the map key "customers" along with the data returned in step 1.1.2
- Example Response:
{
"customers": [
{
"id": 1,
"firstName": "jeff",
"lastName": "Anderson"
},
{
"id": 2,
"firstName": "Jeremy",
"lastName": "Schmersal"
}
]
}
Get a customer by ID or return HttpStatus.NOT_FOUND
if no customer exists with that ID
- Example Request:
GET localhost:8080/api/customers/1
- Step 1.1.4 creates a response entity with data from step 1.1.3 and
HttpStatus.OK
if step 1.1.2 returns true - Step 1.1.5 creates a response entity with
HttpStatus.NOT_FOUND
if step 1.1.2 returns false - Example Response:
{
"id": 1,
"firstName": "jeff",
"lastName": "Anderson"
}
- Example Request:
POST: localhost:8080/api/customers
- Example Request Headers:
Content-Type: application/json
- Example Request Body:
{
"firstName": "Jeremy",
"lastName": "Schmersal",
"emailAddress": "bar@foo.com"
}
- The code below can be used to create and return the location header as shown in steps 1.1.2 - 1.1.7:
UriComponents uriComponents = b.path("/api/customers/{id}").buildAndExpand(newCustomer.getId());
return ResponseEntity.created(uriComponents.toUri()).build();
- Example Response Headers:
Location: http://localhost:8080/api/customers/1
- The response body is empty
Save changes to an existing customer
- Example Request:
PUT: localhost:8080/api/customers/2
- Example Request Headers:
Content-Type: application/json
- Example Request Body:
{
"firstName": "Rod",
"lastName": "Johnson",
"emailAddress": "rod.johnson@spring.io"
}
- Step 1.1.4 creates a response entity with the return data and
HttpStatus.NO_CONTENT
if step 1.1.1 returns true - Step 1.1.5 creates a response entity with
HttpStatus.NOT_FOUND
if step 1.1.1 returns false - The response body is empty
- Example Request:
DELETE: localhost:8080/api/customers/2
- Step 1.1.5 creates a response entity with the return data and
HttpStatus.NO_CONTENT
if step 1.1.2 returns true - Step 1.1.6 creates a response entity with
HttpStatus.NOT_FOUND
if step 1.1.2 returns false - The response body is empty
This won't be graded but will demonstrate your understanding of Java Bean validation in REST controllers
- Open CustomerControllerValidationTests
- One by one, uncomment each test, changing just enough code or configuration to make the test pass
- Once all tests are passing, commit your changes
- Push your changes to GitHub
- Create a pull request for your branch using these instructions
- Submit the assignment in Blackboard as described in these instructions
NOTE: I will provide feedback via. comments in your pull request. If you need to amend your work after you issue your initial pull request:
- Commit your updates
- Push your changes to gitHub
- Verify the new commits were automatically added to your open pull request