CRUD REST API with Spring Boot
In IntelliJ, create a new Spring Boot 4 project that uses JDK25 and Java 25 with the only dependency being Spring Web.
Exercise 1 - Nested JSON Response
Goal: Learn how to create nested JSON responses using Java classes.
-
Create a new class named
Addresswith the following fields:street(String)city(String)zipCode(String)
-
Create another class named
Userwith the following fields:id(Long)name(String)email(String)address(Address)
-
Create a new class named
UserController(make it a rest controller), add a new endpointGET /api/users/{id}that returns aUserobject for the givenid(the data can be hardcoded for this exercise). The JSON response should look like this:{"id": 1,"name": "John Doe","email": "jd@ek.dk","address": {"street": "Guldbergsgade 29","city": "Copenhagen","zipCode": "2200"}} -
Test the endpoint using your browser or Postman by navigating to
http://localhost:8080/api/users/1.
Exercise 2 - List of Objects in JSON Response
Goal: Learn how to return a list of objects in a JSON response.
-
Create a list of
Userobjects in theUserController. -
In the
UserController, add a new endpointGET /api/usersthat returns a list ofUserobjects with sample data (at least 3 users), each containing a nestedAddressobject.[{"id": 1,"name": "John Doe","email": "jd@ek.dk","address": {"street": "Guldbergsgade 29","city": "Copenhagen","zipCode": "2200"}},{"id": 2,"name": "Jane Smith","email": "js@ek.dk","address": {"street": "Amagerbrogade 100","city": "Copenhagen","zipCode": "2300"}}] -
Test the endpoint using your browser or Postman by navigating to
http://localhost:8080/api/users. -
Refactor the
api/users/{id}endpoint to retrieve the user from the list based on the providedid. If the user is not found, return a404 Not Foundresponse.- Hint: Use a loop to find the user
- Hint: Use
ResponseEntity.notFound().build()for 404 responses
Exercise 3 - Bonus: Customize JSON output order
Goal: Learn how to customize the order of fields in the JSON response.
- In the
Userclass, use a Jackson annotation to specify the order of fields in the JSON output so thatidcomes first, followed byname,email, and thenaddress.- Hint: Use the
@JsonPropertyOrder({"id", "name", "email", "address"})annotation on theUserclass.
- Hint: Use the
- Test the
/api/usersand/api/users/{id}endpoints again to verify that the JSON output reflects the specified field order. - (Optional) Apply similar ordering to the
Addressclass. - Test the endpoints again to verify the changes.
Exercise 4 - POST,PUT, DELETE Operations
Goal: Fully implement CRUD operations for the User resource.
4.1 - POST endpoint
- Create a new endpoint
POST /api/usersin theUserControllerthat accepts aUserobject in the request body and returns the same user with a201 Createdstatus code. The ID should be auto-generated (you can simulate this by incrementing a static variable).- Hint: Use the
@RequestBodyannotation to bind the incoming JSON to aUserobject. - Hint: Use
AtomicLongorLongfor generating unique IDs.
- Hint: Use the
- Test the endpoint using Postman by sending a
POSTrequest tohttp://localhost:8080/api/userswith a JSON body representing a new user (without an ID). Verify that the response contains the created user with an assigned ID. - Add a
Locationheader to the response that points to the URL of the newly created user (e.g.,/api/users/{id}).- Hint: You can use
ResponseEntity.created(URI.create("/api/users/" + user.getId())).body(user)to set theLocationheader.
- Hint: You can use
4.2 - PUT endpoint
- Create a new endpoint
PUT /api/users/{id}in theUserController that accepts aUser` object in the request body and updates the existing user with the specified ID. - If the user with the specified ID does not exist, return a
404 Not Foundresponse. - Test the endpoint using Postman by sending a
PUTrequest tohttp://localhost:8080/api/users/{id}with a JSON body representing the updated user data. Verify that the response contains the updated user.
4.3 - DELETE endpoint
- Create a new endpoint
DELETE /api/users/{id}in theUserControllerthat deletes the user with the specified ID and returns a204 No Contentstatus code. - If the user with the specified ID does not exist, return a
204 No Contentresponse. - Test the endpoint using Postman by sending a
DELETErequest tohttp://localhost:8080/api/users/{id}. Verify that the response status code is204 No Content.