Skip to content

Latest commit

 

History

History
130 lines (86 loc) · 6.72 KB

File metadata and controls

130 lines (86 loc) · 6.72 KB
<style> .box { display: Inline-block; text-align: center; padding: 15px; background-color: #23EB9A; border-radius: 10px; align-items: Center; display: flex; justify-content: center; } </style>

Workshop 2: Building APIs with Spring Boot – Persistent User API


Access and Credentials

Item Detail
User +++@lab.VirtualMachine(desktop1).Username+++
Password +++@lab.VirtualMachine(desktop1).Password+++

Challenge overview

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)

Your tasks

Task 1: GET all and GET by id (25 points)

In UserController.java, implement:

  1. GET /api/users – Return all users with HTTP 200 OK and a JSON list.
  2. 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.


Task 2: POST – Create user (25 points)

Implement POST /api/users:

  1. Accept a JSON request body that maps to User (name, username, email, phone, website; id may be null).
  2. Save the user using service.create(user).
  3. 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"
}

Task 3: DELETE (25 points)

Implement DELETE /api/users/{id}:

  1. Delete the user with the given id using service.delete(id).
  2. 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.


Task 4: Testing and documentation (25 points)

  1. Run the application and test all endpoints with Hoppscotch (use the Browser Extension for localhost) or Postman.
  2. Verify: POST returns 201 and the created user; GET all returns 200 and a list; GET by id returns 200; DELETE returns 204.
  3. Document the request URL, method, and response status for each test (e.g. in a short text file or screenshot).

Success criteria

  • 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 User for POST.
  • API is testable with Hoppscotch or Postman.

Running the project

./mvnw spring-boot:run

The API runs on port 8080. Example requests:

  • GET http://localhost:8080/api/users
  • GET http://localhost:8080/api/users/1
  • POST http://localhost:8080/api/users with 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


Resources

@lab.Activity()