Quickstart in Couchbase with Java and Spring Data

  • Learn to build a REST API in Java using Spring Data and Couchbase
  • Explore key-based operations and SQL++ querying using Spring Data Couchbase repositories
  • Explore CRUD operations in action with Couchbase

Come iniziare

Prerequisiti

Per eseguire questo progetto preconfigurato, avrete bisogno di:

  • Couchbase Capella cluster with Travel-sample La carretta è carica.
    • Per eseguire questo tutorial utilizzando un cluster di Couchbase gestito autonomamente, si prega di consultare la documentazione relativa al comando appendice.
  • Git
  • Code Editor or an Integrated Development Environment (e.g., Eclipse)
  • Java SDK 17 or higher installed
  • Gradle Build Tool

Source Code

The sample source code used in this tutorial is published on GitHub. To obtain it, clone the git repository with your IDE or execute the following command:

git clone https://github.com/couchbase-examples/java-springdata-quickstart.git

Installare le dipendenze

Gradle dependencies:

implementation 'org.springframework.boot:spring-boot-starter-web'
// spring data couchbase connector
implementation 'org.springframework.boot:spring-boot-starter-data-couchbase'
// swagger ui
implementation 'org.springdoc:springdoc-openapi-ui:1.6.6'

Database Server Configuration

Spring Data Couchbase connector can be configured by providing a @Configuration bean that extends AbstractCouchbaseConfiguration.

The sample application provides a modernized configuration bean with improved error handling and environment variable support:

@Slf4j
@Configuration
@EnableCouchbaseRepositories
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {

  @Value("#{systemEnvironment['DB_CONN_STR'] ?: '${spring.couchbase.bootstrap-hosts:localhost}'}")
  private String host;

  @Value("#{systemEnvironment['DB_USERNAME'] ?: '${spring.couchbase.bucket.user:Administrator}'}")
  private String username;

  @Value("#{systemEnvironment['DB_PASSWORD'] ?: '${spring.couchbase.bucket.password:password}'}")
  private String password;

  @Value("${spring.couchbase.bucket.name:travel-sample}")
  private String bucketName;

  @Override
  public String getConnectionString() {
    return host;
  }

  @Override
  public String getUserName() {
    return username;
  }

  @Override
  public String getPassword() {
    return password;
  }

  @Override
  public String getBucketName() {
    return bucketName;
  }

  @Override
  public String typeKey() {
    return "type";
  }

  @Override
  @Bean(destroyMethod = "disconnect")
  public Cluster couchbaseCluster(ClusterEnvironment couchbaseClusterEnvironment) {
    try {
      log.debug("Connecting to Couchbase cluster at " + host);
      return Cluster.connect(getConnectionString(), getUserName(), getPassword());
    } catch (Exception e) {
      log.error("Error connecting to Couchbase cluster", e);
      throw e;
    }
  }

  @Bean
  public Bucket getCouchbaseBucket(Cluster cluster) {
    try {
      if (!cluster.buckets().getAllBuckets().containsKey(getBucketName())) {
        log.error("Bucket with name {} does not exist. Creating it now", getBucketName());
        throw new BucketNotFoundException(bucketName);
      }
      return cluster.bucket(getBucketName());
    } catch (Exception e) {
      log.error("Error getting bucket", e);
      throw e;
    }
  }

}

from config/CouchbaseConfiguration.java

These methods are used to configure and retrieve a Couchbase Cluster and a specific Bucket within that cluster in a Spring application.

  • couchbaseCluster(ClusterEnvironment couchbaseClusterEnvironment): This method configures and returns a Cluster instance using the provided ClusterEnvironment. It logs a debug message indicating the connection attempt to the Couchbase cluster. If an error occurs during the connection attempt, it logs an error message and rethrows the exception.

  • getCouchbaseBucket(Cluster cluster): This method retrieves a specific Bucket from the given Cluster. It first checks if the bucket exists in the cluster by calling cluster.buckets().getAllBuckets().containsKey(getBucketName()). If the bucket does not exist, it logs an error message, throws a BucketNotFoundException, and stops the application startup. Otherwise, it returns the Bucket instance.

This default configuration assumes that you have a locally running Couchbae server and uses standard administrative login and password for demonstration purpose. Applications deployed to production or staging environments should use less privileged credentials created using Role-Based Access Control. Please refer to Managing Connections using the Java SDK with Couchbase Server for more information on Capella and local cluster connections.

Application Properties

You need to configure the connection details to your Couchbase Server in the application.properties file located in the src/main/resources directory.s

Modern Spring Boot 3.5+ Configuration:

# Modern Couchbase configuration 
spring.couchbase.connection-string=${DB_CONN_STR}
spring.couchbase.username=${DB_USERNAME}
spring.couchbase.password=${DB_PASSWORD}
spring.couchbase.bucket.name=travel-sample

# Connection optimizations
spring.couchbase.env.timeouts.query=30000ms
spring.couchbase.env.timeouts.key-value=5000ms
spring.couchbase.env.timeouts.connect=10000ms

Environment Variables Setup

For security, use a .env file in your project root:

DB_CONN_STR=couchbases://xyz.cloud.couchbase.com
DB_USERNAME=your_username
DB_PASSWORD=your_password

The connection string formats:

  • Couchbase Capella: couchbases://xyz.cloud.couchbase.com (secure)
  • Local Couchbase: couchbase://localhost (non-secure)

Il couchbases:// protocol is used for secure TLS connections (required for Couchbase Capella). The couchbase:// protocol is for non-secure local connections.

Lanciare l'applicazione

Directly on Machine

In questo momento abbiamo installato le dipendenze, caricato i dati del travel-sample e configurato l'applicazione con le credenziali. L'applicazione è ora pronta e potete lanciarla.

./gradlew bootRun

Note: If you’re using Windows, you can run the application using the Gradle wrapper batch file:

.gradlew.bat bootRun

Usare Docker

Creare l'immagine Docker

docker build -t java-springdata-quickstart .

Iniziare a eseguire l'immagine Docker

docker run -d --name springdata-container -p 8080:8080 java-springdata-quickstart

Nota: Il application.properties file has the connection information to connect to your Capella cluster. You can also pass the connection information as environment variables to the Docker container. If you choose not to pass the environment variables, you can update the application.properties file in the src/main/resources cartella.

Once the application is running, you can see the logs in the console. You should see the following log message indicating that the application has started successfully:

Spring Data Application

Once the site is up and running, you can launch your browser and go to the Swagger Start Page per testare le API.

Swagger UI

Modello dei dati

Per questo tutorial utilizzeremo tre collezioni:, aeroporto, aerolinea e itinerario che contengono rispettivamente aeroporti campione, compagnie aeree e rotte aeree. La raccolta di rotte collega gli aeroporti e le compagnie aeree come mostrato nella figura sottostante. Usiamo queste connessioni nel quickstart per generare aeroporti direttamente collegati e compagnie aeree che collegano un aeroporto di destinazione. Nota che questi sono solo esempi per evidenziare come è possibile utilizzare le query SQL++ per aggregare le collezioni.

Data Model

Struttura dei documenti dell'aerolinea

We will be setting up a REST API to manage some airline documents. Our airline document will have a structure similar to the following:

{
  "name": "Couchbase Airways",
  "callsign": "Couchbase",
  "iata": "CB",
  "icao": "CBA",
  "country": "United States"
}

Il nome field is the name of the airline. The callsign field is the callsign of the airline. The iata field is the IATA code of the airline. The icao field is the ICAO code of the airline. The paese field is the country of the airline.

Let’s Review the Code

To begin clone the repo and open it up in the IDE of your choice to learn about how to create, read, update and delete documents in your Couchbase Server.

Code Organization

  • src/test/java: Contains integration tests.
  • src/main/java/org/couchbase/quickstart/springdata/repository: Contains the repository implementation.
  • src/main/java/org/couchbase/quickstart/springdata/model: Contains the data model.
  • src/main/java/org/couchbase/quickstart/springdata/controller: Contains the RESTful API controllers.
  • src/main/java/org/couchbase/quickstart/springdata/service: Contains the service classes.

Repository

AirlineRepository.java This interface extends the CouchbaseRepository interface and provides methods for CRUD operations.

  • @Scope("..."): Specifies the scope of the repository, which helps organize and manage documents within a Couchbase bucket.

  • @Collection("..."): Specifies the collection within the bucket where the documents are stored.

  • @Repository: Marks the interface as a repository component in the Spring application context.

  • @ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS): Specifies the scan consistency level for queries executed by methods in this repository, ensuring strong consistency for read operations.

Model

Airline.java This class represents an airline document. The @Document annotation indicates that this class is a Couchbase document. The @Field annotation indicates that the following fields are Couchbase document fields: nome, callsign, iata, icao, paese.

Controller

AirlineController.java This class contains the REST API endpoints for CRUD operations. The @RestController annotation indicates that this class is a REST controller. The @RequestMapping annotation specifies the base URL for the REST API. The @Autowired annotation is used to autowire the AirlineService object. The @GetMapping, @PostMapping, @PutMapping, e @DeleteMapping annotations are used to map HTTP GET, POST, PUT, and DELETE requests respectively to their corresponding methods.

Servizio

AirlineService.java This class contains the business logic for the REST API. The @Autowired annotation is used to autowire the AirlineRepository object.

Mapping Workflow

Mapping workflows describe how the HTTP methods (GET, POST, PUT, DELETE) interact with the AirlineService and the underlying database through the AirlineRepository to perform various operations on airline data.

A simple REST API using Spring Boot and the Couchbase SDK version 3.x with the following endpoints:

GET Mapping Workflow

  • The client initiates a GET request to /api/v1/airline/{id}, providing the unique identifier ({id}) of the airline they want to retrieve.
  • Il AirlineController receives the request and invokes the getAirlineById(id) il metodo nel AirlineService. This function internally calls the findById method of the AirlineRepository(which extends CouchbaseRepository) to retrieve the airline information from the Couchbase database.
  • Inside the AirlineService, the request is processed. The service interacts with the AirlineRepository.
  • Il AirlineRepository executes a query against the Couchbase database to retrieve the airline information based on the provided ID.
  • If the airline is found, the AirlineService returns the retrieved information to the AirlineController.
  • Il AirlineController sends an HTTP response with an HTTP status code of 200 (OK) and includes the airline information in the response body.
  • If the airline is not found in the database, the AirlineService returns null.
  • Il AirlineController responds with an HTTP status code of 404 (Not Found) if the airline is not found.

POST Mapping Workflow

  • The client initiates a POST request to /api/v1/airline/{id} with a JSON payload containing the airline’s information, including a unique ID.
  • Il AirlineController receives the request and invokes the createAirline(airline) il metodo nel AirlineService. The createAirline method internally calls the save method of the AirlineRepository to save the airline information to the Couchbase database.
  • Inside the AirlineService, the incoming data is validated to ensure it meets the required criteria.
  • Il AirlineService creates a new Aerea object and saves it to the Couchbase database using the AirlineRepository.
  • If the airline is successfully created, the AirlineService returns the newly created airline object.
  • Il AirlineController sends an HTTP response with an HTTP status code of 201 (Created), including the newly created airline information in the response body.
  • If the airline already exists in the database, a DocumentExistsException may be thrown.
  • In case of a conflict, the AirlineController responds with an HTTP status code of 409 (Conflict).

PUT Mapping Workflow

  • The client initiates a PUT request to /api/v1/airline/{id} with a JSON payload containing the updated airline information and the unique ID of the airline to be updated.
  • Il AirlineController receives the request and invokes the updateAirline(id, airline) il metodo nel AirlineService. The aggiornamentoAirline method internally calls the save method of the AirlineRepository to update the airline information in the Couchbase database.
  • Inside the AirlineService, the incoming data is validated to ensure it meets the required criteria.
  • Il AirlineService updates the airline record in the Couchbase database using the AirlineRepository.
  • If the airline is found and updated successfully, the AirlineService returns the updated airline object.
  • Il AirlineController sends an HTTP response with an HTTP status code of 200 (OK), including the updated airline information in the response body.
  • If the airline is not found in the database, the AirlineService returns null.
  • In case of an update to a non-existent airline, the AirlineController responds with an HTTP status code of 404 (Not Found).

DELETE Mapping Workflow

  • The client initiates a DELETE request to /api/v1/airline/{id}, specifying the unique identifier ({id}) of the airline to be deleted.
  • Il AirlineController receives the request and invokes the deleteAirline(id) il metodo nel AirlineService. The deleteAirline method internally calls the deleteById method of the AirlineRepository to delete the airline from the Couchbase database.
  • Inside the AirlineService, the service attempts to find and delete the airline record from the Couchbase database using the AirlineRepository.
  • If the airline is found and successfully deleted, the AirlineService completes the operation.
  • Il AirlineController responds with an HTTP status code of 204 (No Content) to indicate a successful deletion.
  • If the airline is not found in the database, the AirlineService may throw a Errore DocumentNotFound.
  • In case the airline is not found, the AirlineController responds with an HTTP status code of 404 (Not Found).

These detailed workflows provide a comprehensive understanding of how each mapping is handled by the controller, service, and repository components in your Spring Data project.

Custom SQL++ Queries

Il AirlineRepository interface contains a @Query annotation that allows us to create custom N1QL queries. The @ScanConsistency annotation allows us to specify the scan consistency for the query. The @Param annotation allows us to specify parameters for the query.

@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND country = $country")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airline> findByCountry(@Param("country") String country);

from repository/AirlineRepository.java

Il findByCountry method returns a list of airlines by country. It uses the @Query annotation to create a custom N1QL query. The @Param annotation is used to specify the paese parameter for the query. The @ScanConsistency annotation is used to specify the scan consistency for the query.

@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND ANY destination IN routes SATISFIES destination = $airport END")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airline> findByDestinationAirport(@Param("airport") String airport);

from repository/AirlineRepository.java

Il findByDestinationAirport method returns a list of airlines by destination airport. It uses the @Query annotation to create a custom N1QL query. The @Param annotation is used to specify the aeroporto parameter for the query. The @ScanConsistency annotation is used to specify the scan consistency for the query.

For more information, see the Couchbase Java SDK documentation.

Running The Tests

Integration Tests

This project uses Gradle with comprehensive integration test setup. The tests connect to a real Couchbase instance:

./gradlew test

Windows users:

.gradlew.bat test

Test Reliability

This project was modernized to ensure reliable test execution:

  • Tests properly connect to Couchbase and validate real data
  • Integration tests are included in the standard test run
  • Test logging provides clear visibility into execution

Continuous Integration

GitHub Actions

The project includes a modern GitHub Actions workflow with:

  • Multi-Version Testing: Java 17 and 21 support
  • Manual Triggering: workflow_dispatch for on-demand runs
  • Enhanced Reliability: 15-minute timeouts, non-blocking failure handling
  • Test Artifacts: Automatic test report uploads
  • Optimized Commands: Uses ./gradlew clean test --stacktrace

CI/CD Features

The workflow provides:

  1. Temurin JDK setup with Gradle caching
  2. Real integration test execution against Couchbase
  3. Test result artifact collection for debugging
  4. Slack notification integration (configurable)

Project Setup Notes

This project was created using the Spring Initializr and modernized with:

  • Project: Gradle Project with Wrapper
  • Language: Java
  • Spring Boot: 3.5.5 (upgraded from 2.7.18)
  • Packaging: Jar
  • Java Version: 17+ (upgraded from Java 8)
  • Key Dependencies:
    • Spring Boot Starter Web
    • Spring Boot Starter Data Couchbase
    • SpringDoc OpenAPI Starter WebMVC UI
    • Spring Boot Starter Validation
    • Project Lombok
    • JUnit 5 (Jupiter)

Performance Optimizations: The project uses Slice instead of Page for better memory efficiency and eliminates expensive COUNT queries in pagination scenarios.

Contributing

Contributions are welcome! If you’d like to contribute to this project, please fork the repository and create a pull request.

Appendice

Ampliare l'API aggiungendo un'entità nuova

Se desiderate aggiungere un'altra entità alle API, i passaggi da seguire sono i seguenti:

  • Creare la nuova entità (collezione) nel bucket Couchbase. È possibile creare la collezione utilizzando il comando SDK o tramite il Interfaccia del server Couchbase.
  • Define the mappings in a new file in the controllers folder similar to the existing mappings like AirportController.java.
  • Define the service in a new file in the services folder similar to the existing services like AirportService.java.
  • Define the repository in a new file in the repositories folder similar to the existing repositories like AirportRepository.java.
  • For integration tests, create a new class in the controllers package similar to the existing tests like AirportIntegrationTest.java.

Gestione automatica di un cluster di Couchbase

Se stai eseguendo questo quickstart con un cluster di Couchbase gestito autonomamente, è necessario caricare caricare i dati del bucket dei dati di viaggio nel cluster e generare le credenziali per il bucket.

È necessario aggiornare la stringa di connessione e le credenziali nel file src/main/resources/application.properties file.

NOTA: Couchbase must be installed and running prior to running the Spring Boot app.

Documentazione Swagger

La documentazione di Swagger fornisce una panoramica chiara dell'API, includendo endpoint, metodi HTTP, parametri delle richieste e oggetti di risposta.

Fare clic su un singolo endpoint per ampliarlo e visualizzare le informazioni dettagliate. Ciò include la descrizione dell’endpoint, i possibili codici di stato delle risposte e i parametri della richiesta che esso accetta.

Testare l'API

È possibile provare un'API cliccando sul pulsante “Prova” accanto agli endpoint.

  • Parametri: Se un endpoint richiede parametri, l'interfaccia utente Swagger fornisce dei campi di input da compilare. Questi possono includere parametri di percorso, stringhe di query, intestazioni o il corpo di una richiesta POST/PUT.

  • Esecuzione: Una volta inseriti tutti i parametri necessari, è possibile cliccare sul pulsante “Esecuzione” per effettuare una chiamata API in tempo reale. L’interfaccia utente Swagger invierà la richiesta all’API e visualizzerà la risposta direttamente nella documentazione. Ciò include il codice della risposta, gli header e il corpo della risposta.

Modelli

Swagger documenta la struttura dei corpi di richiesta e risposta utilizzando modelli. Questi modelli definiscono la struttura dei dati prevista utilizzando lo schema JSON e sono estremamente utili per comprendere quali dati inviare e quali aspettarsi.

Lasciare la proliferazione dei database nel passato

Ogni aggiunta di stack significa un altro conto da gestire. Calcola in 2 minuti quanto puoi risparmiare.