| Item | Detail |
|---|---|
| User | +++@lab.VirtualMachine(desktop1).Username+++ |
| Password | +++@lab.VirtualMachine(desktop1).Password+++ |
When: This is the Workshop Skills Application. Complete it after Workshop 2 has ended, in the two hours that follow.
You will complete a RESTful API in Spring Boot that performs CRUD operations on a User resource (modelled after JSONPlaceholder users). The project already has User entity, repository, service, and controller structure; you will implement the controller endpoints with the correct HTTP methods, path variables, request body, and status codes.
Learning objectives:
- Design and implement RESTful API endpoints using Spring Boot controllers
- Develop endpoints for Create, Read, and Delete with appropriate HTTP methods and status codes
- Process request data using @PathVariable and @RequestBody
- Generate structured responses with appropriate status codes (200, 201, 204)
In UserController.java, implement:
- GET /api/users – Return all users with HTTP 200 OK and a JSON list.
- GET /api/users/{id} – Return the user with the given id. If found, return 200 OK and the user JSON. If not found, return 404 Not Found.
Use the existing UserService methods getAll() and getById(Long id). For getById, the service throws if not found; you may catch and return 404, or add a findOptional-style method if you prefer. Return ResponseEntity with the correct status and body.
Implement POST /api/users:
- Accept a JSON request body that maps to
User(name, username, email, phone, website; id may be null). - Save the user using
service.create(user). - Return 201 Created with the saved user in the response body (including the generated id).
Example JSON body:
{
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"phone": "1-770-736-8031"
}Implement DELETE /api/users/{id}:
- Delete the user with the given id using
service.delete(id). - Return 204 No Content (no response body).
If the service throws when the user is not found, you may add error handling to return 404 Not Found for that case.
- Run the application and test all endpoints with Hoppscotch (use the Browser Extension for localhost) or Postman.
- Verify: POST returns 201 and the created user; GET all returns 200 and a list; GET by id returns 200; DELETE returns 204.
- Document the request URL, method, and response status for each test (e.g. in a short text file or screenshot).
- All four endpoints (GET all, GET by id, POST, DELETE) behave as specified.
- Status codes: 200 (GET), 201 (POST), 204 (DELETE), and 404 when resource is missing (if implemented).
- Request body is correctly bound to
Userfor POST. - API is testable with Hoppscotch or Postman.
./mvnw spring-boot:runThe API runs on port 8080. Example requests:
GET http://localhost:8080/api/usersGET http://localhost:8080/api/users/1POST http://localhost:8080/api/userswith JSON body:{"name":"Leanne Graham","username":"Bret","email":"Sincere@april.biz","phone":"1-770-736-8031"}DELETE http://localhost:8080/api/users/1
Hoppscotch: Use the Hoppscotch Browser Extension and set Interceptor to Browser Extension so localhost requests work.
H2 console (if enabled): http://localhost:8080/h2-console
- Starter code: This repository (controller methods to complete).
- Spring Boot REST: https://spring.io/guides/gs/rest-service/
- ResponseEntity: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html
@lab.Activity()