A minimal Spring Boot REST API that persists User records to a local PostgreSQL database via Spring Data JPA. Built as part of the Ada School "Spring Boot con Postgres" codelab.
- Java 17
- Spring Boot 2.7.18 (
spring-boot-starter-web,spring-boot-starter-data-jpa) - PostgreSQL JDBC driver
- Maven
- Hibernate ORM (dialect auto-detected)
src/main/java/org/adaschool/datapostgres
├── DataPostgresApplication.java # application entry point
├── controller/
│ └── UserController.java # REST endpoints
└── data/
├── dto/UserDto.java # request payload
├── entity/User.java # JPA entity mapped to the "users" table
├── exception/UserNotFoundException.java
└── repository/UserRepository.java # Spring Data JPA repository
- JDK 17+
- Maven 3.6+
- A running local PostgreSQL server (any version) with a user/password and an existing database created for this project
Create a role and database (adjust names/password as you like):
CREATE ROLE codelab_user LOGIN PASSWORD 'codelab123';
CREATE DATABASE codelab OWNER codelab_user;Connection settings live in src/main/resources/application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/codelab
spring.datasource.username=codelab_user
spring.datasource.password=codelab123
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=trueUpdate url, username, and password to match your own local PostgreSQL setup. spring.jpa.hibernate.ddl-auto=update lets Hibernate create/update the users table automatically on startup, and data persists across restarts.
mvn spring-boot:runThe API starts on http://localhost:8080 (override with --server.port=<port> if 8080 is taken). On first boot, Hibernate creates the users table in the configured database.
POST /v1/user
Content-Type: application/json
{
"name": "Ada Lovelace",
"email": "ada@example.com"
}
Response 200 OK
{
"id": 1,
"name": "Ada Lovelace",
"email": "ada@example.com"
}GET /v1/user/{id}
Response 200 OK — same shape as above.
Response 404 Not Found if no user exists with that id.
- Start the app and create a user via
POST /v1/user. - Stop the app (
Ctrl+C). - Start it again with
mvn spring-boot:run. GET /v1/user/{id}for the user created in step 1 — it should still be returned, confirming the data survived the restart in PostgreSQL.
mvn clean packageProduces an executable jar at target/data-postgres-0.0.1-SNAPSHOT.jar, runnable with java -jar target/data-postgres-0.0.1-SNAPSHOT.jar.