| Item | Detail |
|---|---|
| User | +++@lab.VirtualMachine(desktop1).Username+++ |
| Password | +++@lab.VirtualMachine(desktop1).Password+++ |
When: Complete this lab before the Unit 2 workshop. It is a precursor to the workshop demo: you will build a minimal User API so that the live session builds on what you have already tried.
Goal: Create a Spring Boot application that exposes two endpoints (GET all users, POST create user) backed by an in-memory H2 database. You will use this same stack in the workshop, where the instructor will add full CRUD and Hoppscotch testing.
Tools: start.spring.io, Java IDE (e.g. IntelliJ), Hoppscotch (with Browser Extension for localhost) or Postman.
- Bootstrap a Spring Boot project with Spring Web, Spring Data JPA, H2, and Lombok
- Configure H2 in
application.properties - Create a JPA entity, a repository, a service, and a REST controller
- Expose GET (all) and POST (create) for a User resource and test with an API client
- Go to start.spring.io.
- Choose Maven, Java, and Spring Boot 4.0.x (or latest stable).
- Add dependencies: Spring Web, Spring Data JPA, H2 Database, Lombok.
- Generate the project, extract it, and open it in your IDE.
- Open src/main/resources/application.properties.
- Add:
spring.application.name=user-service
spring.datasource.url=jdbc:h2:mem:userdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true- Create package model under
src/main/java/com/example/demo/. - Create User.java with fields:
id(Long, generated),name,username,email(all required where appropriate), and optionalphone,website. Use@Entity,@Table(name = "users"), and Lombok@Data,@NoArgsConstructor,@AllArgsConstructor. Use@Column(unique = true, nullable = false)forusernameandemail.
- Create package repository and interface UserRepository extending
JpaRepository<User, Long>. - Create package service and class UserService with constructor injection of
UserRepository. Implement:getAll()– returnrepository.findAll()create(User user)– returnrepository.save(user)
- Create package controller and class UserController with
@RestControllerand@RequestMapping("/api/users"). - Implement:
- GET /api/users – return
List<User>(all users). - POST /api/users – accept
@RequestBody User user, callservice.create(user), and return the created user with HTTP status 201 Created (useResponseEntity).
- GET /api/users – return
- Install the Hoppscotch Browser Extension and set Interceptor to Browser Extension (so localhost works), or use Postman.
- Run the application from your IDE.
- POST to
http://localhost:8080/api/userswith body (application/json):
{
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"phone": "1-770-736-8031"
}- Expect 201 Created and the created user (with generated
id) in the response.
- GET
http://localhost:8080/api/users– expect an array containing that user.
- The application starts without errors.
- POST returns 201 and the saved user with an
id. - GET /api/users returns a list including the created user.
- You are ready to follow the workshop demo, which will add GET by ID, DELETE, and full Hoppscotch testing.
@lab.Activity()