Sub topic 1
Building a CRUD API with Spring Boot
Prerequisites
Assume Java fundamentals, basic HTTP and JSON, relational database concepts, and familiarity with Maven or Gradle.
REST and CRUD: mapping operations to HTTP
REST models application data as resources. A CRUD API supports create, read, update, and delete operations. HTTP methods communicate the intended operation.
| CRUD operation | HTTP method | Example endpoint | Usual response |
|---|---|---|---|
| Create | POST | `/api/books` | `201 Created` |
| Read one | GET | `/api/books/7` | `200 OK` |
| Update | PUT | `/api/books/7` | `200 OK` |
| Delete | DELETE | `/api/books/7` | `204 No Content` |
A collection endpoint such as GET /api/books returns multiple resources. The controller will translate HTTP requests into application calls. Later chapters add validation and consistent error responses.
flowchart LR
Client --> Controller
Controller --> Service
Service --> Repository
Repository --> DatabaseCreating the project and choosing starters
Create a Spring Boot project with Spring Initializr. Select Java, Maven, and the required starters:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>Spring Web handles HTTP. Spring Data JPA provides database access. Validation checks request data. H2 is convenient for local development. Use PostgreSQL or another production database for deployment.
Configuring the database connection
For PostgreSQL, put connection settings in application.properties. Read credentials from environment variables rather than committing them:
spring.datasource.url=jdbc:postgresql://localhost:5432/books
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.open-in-view=falseddl-auto=validate checks that the existing schema matches the entity. It does not silently change the database.
Modeling the resource as a JPA entity
A JPA entity maps a Java object to a database table:
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
protected Book() { }
public Book(String title, String author) {
this.title = title;
this.author = author;
}
}JPA requires a no-argument constructor. The generated identifier is assigned by the database.
Data access with a Spring Data repository
A repository defines the entity type and identifier type:
public interface BookRepository
extends JpaRepository<Book, Long> {
}Spring generates the implementation. The repository already supports saving, finding, paging, checking existence, and deleting. Keep database operations behind this interface so the service layer does not depend on SQL details.
Exercise
Which HTTP method and status code should create a new book?
Show answer
Use POST on /api/books and return 201 Created, usually with a Location header for the new resource.