Skip to content

Latest commit

 

History

History
126 lines (88 loc) · 6.61 KB

File metadata and controls

126 lines (88 loc) · 6.61 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>

Unit 2: Spring Boot User API – Precursor Lab (Async)


Access and Credentials

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

Challenge overview

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.


Learning objectives

  • 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

Task 1: Project setup

  1. Go to start.spring.io.
  2. Choose Maven, Java, and Spring Boot 4.0.x (or latest stable).
  3. Add dependencies: Spring Web, Spring Data JPA, H2 Database, Lombok.
  4. Generate the project, extract it, and open it in your IDE.

Task 2: Database configuration

  1. Open src/main/resources/application.properties.
  2. 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

Task 3: User entity

  1. Create package model under src/main/java/com/example/demo/.
  2. Create User.java with fields: id (Long, generated), name, username, email (all required where appropriate), and optional phone, website. Use @Entity, @Table(name = "users"), and Lombok @Data, @NoArgsConstructor, @AllArgsConstructor. Use @Column(unique = true, nullable = false) for username and email.

Task 4: Repository and service

  1. Create package repository and interface UserRepository extending JpaRepository<User, Long>.
  2. Create package service and class UserService with constructor injection of UserRepository. Implement:
    • getAll() – return repository.findAll()
    • create(User user) – return repository.save(user)

Task 5: REST controller (GET all, POST create)

  1. Create package controller and class UserController with @RestController and @RequestMapping("/api/users").
  2. Implement:
    • GET /api/users – return List<User> (all users).
    • POST /api/users – accept @RequestBody User user, call service.create(user), and return the created user with HTTP status 201 Created (use ResponseEntity).

Task 6: Test with Hoppscotch or Postman

  1. Install the Hoppscotch Browser Extension and set Interceptor to Browser Extension (so localhost works), or use Postman.
  2. Run the application from your IDE.
  3. POST to http://localhost:8080/api/users with 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.
  1. GET http://localhost:8080/api/users – expect an array containing that user.

Success criteria

  • 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()