When a catalog lives in a database, two jobs remain: create the tables, and read the rows with . Flyway applies those table changes in version order when the application starts. JdbcClient is Spring’s class for sending SQL and turning the rows into Java objects. Spring Boot builds a JdbcClient for you once a database is available.

A numbered SQL file runs on startup

Our coffee shop still sells coffee and tea, but the catalog now lives in a database instead of a list in memory. The shop uses H2, a small database that runs inside the app. Spring Boot connects to it by itself, so this example sets no connection URL.

The database starts empty. It needs a products table, with coffee and tea already in it. Each change is a migration: a SQL file whose name carries its version. Flyway runs any that have not been applied yet, in order, and only once.

The file sits in the folder Flyway looks at by default, db/migration. Its name starts with V1, then two underscores, then a short description: V1__create_products.sql. V1 means version 1. Inside, one statement creates the table, and two more insert the rows:

CREATE TABLE products (
    id   BIGINT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

INSERT INTO products (id, name) VALUES (1, 'Coffee');
INSERT INTO products (id, name) VALUES (2, 'Tea');

V1__create_products.sql

This Product has an id and a name only; it has no price. When the shop starts, the log says Flyway is migrating to version 1, create products, and that the migration was applied successfully. Because this H2 database lives in memory, it starts empty on every start, and Flyway fills it again each time. The Database Initialization guide covers how Boot runs Flyway, and where to put later versions such as V2__....

Map rows to records with JdbcClient

Java still needs to read those rows. Spring Boot already built a JdbcClient, connected to our H2 database, so the catalog controller asks for one in its constructor and Spring passes it in.

GET /catalog calls sql() with plain SQL, then query() with the Product record class, then list():

CatalogController(JdbcClient jdbcClient) {
    this.jdbcClient = jdbcClient;
}

@GetMapping("/catalog")
List<Product> all() {
    return jdbcClient.sql("SELECT id, name FROM products ORDER BY id")
            .query(Product.class)
            .list();
}

CatalogController.java

A record is a short Java class that holds a few values. For each row, JdbcClient makes one Product: the id column fills id, and the name column fills name. GET /catalog returns that list as JSON, 200 OK, with coffee as id 1 and tea as id 2: the rows from the SQL file.

The SQL Databases page explains that Boot auto-configures JdbcClient so you can inject it directly.

One product, or 404

GET /catalog/{id} uses the same SQL, plus a filter: WHERE id = :id. :id is a named placeholder. It stands in for a value that param("id", id) supplies when the query runs, here the id from the URL.

This time the chain ends with optional() instead of list(). An Optional either holds one product or is empty. ResponseEntity holds the answer’s body together with its status. ResponseEntity.of turns a present product into 200 with that product, and an empty Optional into 404 Not Found:

@GetMapping("/catalog/{id}")
ResponseEntity<Product> one(@PathVariable Long id) {
    Optional<Product> product = jdbcClient.sql("SELECT id, name FROM products WHERE id = :id")
            .param("id", id)
            .query(Product.class)
            .optional();
    return ResponseEntity.of(product);
}

CatalogController.java

GET /catalog/1 answers 200 with coffee. GET /catalog/99 answers 404: there is no row with that id.

If the SQL itself fails, Spring does not leave you with a raw database error. It translates the SQLException into a DataAccessException that names the problem more clearly. The JDBC exception translation page covers how that works.

In a finished product, the controller usually would not talk to the database itself. This example keeps them together so the client is easy to see.

Recap

  • Put a numbered SQL file in db/migration, named like V1__create_products.sql. Flyway runs it when the app starts.
  • Inject the JdbcClient Spring Boot builds. Call sql(), then query(YourRecord.class), then list() to map each row to a record.
  • For one row, add a named placeholder, fill it with param(), and end with optional(). ResponseEntity.of turns a product into 200 and an empty result into 404.
  • If the SQL fails, Spring translates the database error into a DataAccessException.
  • Keep the controller and the database together only while you learn the client. In a finished product, a layer usually sits between them.

Sources

Official documentation and well-known tutorials read while writing this article. The wording here is our own.