Often, the first step developers take after creating their database is to create a REST API that can perform Create, Read, Update, and Delete (CRUD) operations for that database. This repo is designed to teach you and give you a starter project (in Java using Spring Boot) to generate such a REST API. After you have loaded the travel-sample bucket in your database, you can run this application which is a REST API with Swagger documentation so that you can learn:
- How to create, read, update, and delete documents using Key-Value operations (KV operations). KV operations are unique to Couchbase and provide super fast (under millisecond) operations.
- How to write simple parametrized N1QL queries using the built-in travel-sample bucket.
Full documentation for the tutorial can be found on the Couchbase Developer Portal.
To run this prebuilt project, you will need:
- Couchbase Capella cluster with travel-sample bucket loaded.
- To run this tutorial using a self-managed Couchbase cluster, please refer to the appendix.
- Java SDK 17+
- Ensure that the Java version is compatible with the Couchbase SDK. Please check the link for compatibility
- Loading Travel Sample Bucket:
- If
travel-sample
is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster. Load travel-sample bucket in Couchbase Capella
- If
- Maven 3.6.3+
We will walk through the different steps required to get the application running.
git clone https://github.com/couchbase-examples/java-springboot-quickstart.git
cd java-springboot-quickstart
The dependencies for the application are specified in the pom.xml
file in the root folder. Dependencies can be installed through mvn
the default package manager for Java.
mvn clean install -DskipTests=true
Note: The -DskipTests=true
option is used to skip the tests. The tests require the application to be running.
Note: The application is tested with Java 17 and 21. If you are using a different version of Java, please update the pom.xml
file accordingly.
To learn more about connecting to your Capella cluster, please follow the instructions.
Specifically, you need to do the following:
- Create the database credentials to access the travel-sample bucket (Read and Write) used in the application.
- Allow access to the Cluster from the IP on which the application is running.
All configuration for communication with the database is read from the application properties. We have provided a convenience feature in this quickstart to read the properties from a local file, application.properties
in the src/main/resources
folder.
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
spring.couchbase.bucket.name=travel-sample
spring.couchbase.connection-string=${DB_CONN_STR}
spring.couchbase.username=${DB_USERNAME}
spring.couchbase.password=${DB_PASSWORD}
The application uses environment variables for database configuration. You can set these in your system environment or create a .env
file in the project root:
DB_CONN_STR=couchbases://your-cluster.cloud.couchbase.com
DB_USERNAME=your-username
DB_PASSWORD=your-password
Note: The connection string expects the
couchbases://
orcouchbase://
part.
The application will automatically load environment variables from a .env
file if present, or use system environment variables.
At this point, we have installed the dependencies, loaded the travel-sample data and configured the application with the credentials. The application is now ready and you can run it.
mvn spring-boot:run
Build the Docker image
docker build -t java-springboot-quickstart .
Run the Docker image
docker run -d --name springboot-container -p 9440:8080 java-springboot-quickstart -e DB_CONN_STR=<connection_string> -e DB_USERNAME=<username> -e DB_PASSWORD=<password>
Note: You can pass the connection information as environment variables to the Docker container or include a .env
file in your Docker build context.
Once the application starts, you can see the details of the application on the logs.
The application will run on port 8080 of your local machine (http://localhost:8080). You will find the interactive Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API endpoints and how they can be invoked. More details on the Swagger documentation can be found in the appendix.
To test your application, you can use Maven (mvn) to run the test suite. Here are the steps to run the tests:
mvn test
This command will execute all the test cases in your project.
Additionally, you can run individual test classes or methods using the following commands:
To run the tests for the AirlineIntegrationTest class:
mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.AirlineIntegrationTest
To run the tests for the AirportIntegrationTest class:
mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.AirportIntegrationTest
To run the tests for the RouteIntegrationTest class:
mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.RouteIntegrationTest
For this quickstart, we use three collections, airport
, airline
and routes
that contain sample airports, airlines and airline routes respectively. The routes collection connects the airports and airlines as seen in the figure below. We use these connections in the quickstart to generate airports that are directly connected and airlines connecting to a destination airport. Note that these are just examples to highlight how you can use SQL++ queries to join the collections.
If you would like to add another entity to the APIs, these are the steps to follow:
- Create a new collection in the Couchbase scope. You can create new collections using the SDK or via the Couchbase Server interface.
- Define the routes in a new class in the
controllers
package similar to the existing routes likeAirportController.java
. - Define the service in a new class in the
services
package similar to the existing services likeAirportService.java
. - Define the repository in a new class in the
repositories
package similar to the existing repositories likeAirportRepository.java
.
If you are running this quickstart with a self-managed Couchbase cluster, you need to load the travel-sample data bucket in your cluster and generate the credentials for the bucket.
You need to set the connection string and credentials using environment variables or a .env
file as described above.
Note: Couchbase Server version 7 or higher must be installed and running before running the Spring Boot Java app.
Swagger documentation provides a clear view of the API including endpoints, HTTP methods, request parameters, and response objects.
Click on an individual endpoint to expand it and see detailed information. This includes the endpoint's description, possible response status codes, and the request parameters it accepts.
You can try out an API by clicking on the "Try it out" button next to the endpoints.
-
Parameters: If an endpoint requires parameters, Swagger UI provides input boxes for you to fill in. This could include path parameters, query strings, headers, or the body of a POST/PUT request.
-
Execution: Once you've inputted all the necessary parameters, you can click the "Execute" button to make a live API call. Swagger UI will send the request to the API and display the response directly in the documentation. This includes the response code, response headers, and response body.
Swagger documents the structure of request and response bodies using models. These models define the expected data structure using JSON schema and are extremely helpful in understanding what data to send and expect.