|
1 | | -# Quickstart in Couchbase with Java and Spring Boot |
2 | | -#### Build a REST API with Couchbase's Java SDK 3 and Spring Boot |
| 1 | +# Quickstart in Couchbase with Spring Boot and Java |
3 | 2 |
|
4 | | -> This repo is designed to teach you how to connect to a Couchbase cluster to create, read, update, and delete documents and how to write simple parametrized N1QL queries. |
| 3 | +#### REST API using Couchbase Capella in Java using Spring Boot |
5 | 4 |
|
6 | | -[](https://gitpod.io/#https://github.com/couchbase-examples/java-springboot-quickstart) |
| 5 | +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 installed 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: |
| 6 | + |
| 7 | +1. How to create, read, update, and delete documents using Key-Value[ operations](https://docs.couchbase.com/java-sdk/current/howtos/kv-operations.html) (KV operations). KV operations are unique to Couchbase and provide super fast (think microseconds) queries. |
| 8 | +2. How to write simple parametrized [N1QL queries](https://docs.couchbase.com/java-sdk/current/howtos/n1ql-queries-with-sdk.html) using the built-in travel-sample bucket. |
| 9 | + |
| 10 | +Full documentation for the tutorial can be found on the [Couchbase Developer Portal](https://developer.couchbase.com/tutorial-quickstart-spring-boot-java/). |
| 11 | + |
| 12 | +If you are looking for a quick start using [Micronaut](https://micronaut.io/)](https://micronaut.io/), you can find it in this [repo](https://github.com/couchbase-examples/java-quickstart-micronaut). |
7 | 13 |
|
8 | | -Full documentation can be found on the [Couchbase Developer Portal](https://developer.couchbase.com/tutorial-quickstart-java-springboot/). |
9 | 14 | ## Prerequisites |
| 15 | + |
10 | 16 | To run this prebuilt project, you will need: |
11 | 17 |
|
12 | | -- Couchbase Server 7 Installed (version 7.0.0-5247 or higher) |
13 | | -- Java SDK v1.8 or higher installed |
14 | | -- Code Editor installed (IntelliJ IDEA, Eclipse, or Visual Studio Code) |
15 | | -- Maven command line |
| 18 | +- [Couchbase Capella](https://www.couchbase.com/products/capella/) cluster with [travel-sample](https://docs.couchbase.com/java-sdk/current/ref/travel-app-data-model.html) bucket loaded. |
| 19 | + - To run this tutorial using a self-managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster). |
| 20 | +- [Java](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) 11 or higher installed |
| 21 | + - Ensure that the Java version is compatible with the Couchbase SDK. |
| 22 | +- Loading Travel Sample Bucket |
| 23 | + If travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster: |
| 24 | + - [Load travel-sample bucket in Couchbase Capella](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html#import-sample-data) |
16 | 25 |
|
17 | | -## Install Dependencies |
| 26 | +## App Setup |
18 | 27 |
|
| 28 | +We will walk through the different steps required to get the application running. |
19 | 29 |
|
20 | | -```sh |
21 | | -mvn package |
| 30 | +### Cloning Repo |
| 31 | + |
| 32 | +```shell |
| 33 | +git clone https://github.com/couchbase-examples/java-springboot-quickstart |
22 | 34 | ``` |
23 | | -> Note: Maven packages auto restore when building the project in IntelliJ IDEA or Eclipse depending on IDE configuration. |
24 | 35 |
|
25 | | -### Database Server Configuration |
| 36 | +### Install Dependencies |
| 37 | + |
| 38 | +The dependencies for the application are specified in the `pom.xml` file in the source folder. Dependencies can be installed through `mvn` the default package manager for Java. |
| 39 | + |
| 40 | +``` |
| 41 | +mvn clean install -DskipTests=true |
| 42 | +``` |
| 43 | + |
| 44 | +Note: The `-DskipTests=true` option is used to skip the tests. The tests require the application to be running. |
| 45 | + |
| 46 | +Note: The application is tested with Java 17. If you are using a different version of Java, please update the `pom.xml` file accordingly. |
| 47 | + |
| 48 | +### Setup Database Configuration |
26 | 49 |
|
27 | | -All configuration for communication with the database is stored in the `/src/main/resources/application.properties` file. This includes the connection string, username, and password. The default username is assumed to be `Administrator` and the default password is assumed to be `password`. If these are different in your environment you will need to change them before running the application. |
| 50 | +To learn more about connecting to your Capella cluster, please follow the [instructions](https://docs.couchbase.com/cloud/get-started/connect.html). |
28 | 51 |
|
29 | | -### Dependency Injection via DBSetupRunner class |
| 52 | +Specifically, you need to do the following: |
30 | 53 |
|
31 | | -The quickstart code provides a CommandLineRunner called DBSetupRunner in the runners folder that wires up the Bucket and Cluster objects for dependency injection. This runner also creates the bucket, collection, scope, and indexes for the tutorial to run properly automatically when the application |
| 54 | +- Create the [database credentials](https://docs.couchbase.com/cloud/clusters/manage-database-users.html) to access the travel-sample bucket (Read and Write) used in the application. |
| 55 | +- [Allow access](https://docs.couchbase.com/cloud/clusters/allow-ip-address.html) to the Cluster from the IP on which the application is running. |
| 56 | + |
| 57 | +All configuration for communication with the database is read from the environment variables. We have provided a convenience feature in this quickstart to read the environment variables from a local file, `application.properties` in the `src/main/resources` folder. |
| 58 | + |
| 59 | +```properties |
| 60 | +spring.couchbase.bootstrap-hosts=####### |
| 61 | +spring.couchbase.bucket.name=travel-sample |
| 62 | +spring.couchbase.bucket.user=####### |
| 63 | +spring.couchbase.bucket.password=####### |
| 64 | +spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER |
| 65 | +``` |
| 66 | + |
| 67 | +Instead of the hash symbols, you need to add the values for the Couchbase connection. |
| 68 | + |
| 69 | +> Note: The connection string expects the `couchbases://` or `couchbase://` part. |
32 | 70 |
|
33 | 71 | ## Running The Application |
34 | 72 |
|
35 | | -At this point the application is ready and you can run it via your IDE or from the terminal: |
| 73 | +### Directly on Machine |
| 74 | + |
| 75 | +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. |
36 | 76 |
|
37 | 77 | ```sh |
38 | | -mvn spring-boot:run -e -X |
| 78 | +mvn spring-boot:run |
39 | 79 | ``` |
40 | 80 |
|
41 | | -You can launch your browser and go to the [Swagger start page](https://localhost:8080/swagger-ui/index.html). |
| 81 | +### Using Docker |
42 | 82 |
|
43 | | -## Running The Tests |
| 83 | +Build the Docker image |
44 | 84 |
|
45 | | -To run the standard integration tests (which requires a running Couchbase Server), use the following command: |
| 85 | +```sh |
| 86 | +docker build -t java-springboot-quickstart . |
| 87 | +``` |
| 88 | + |
| 89 | +Run the Docker image |
46 | 90 |
|
47 | 91 | ```sh |
48 | | -mvn verify |
| 92 | +docker run -d --name springboot-container -p 8080:8080 java-springboot-quickstart |
49 | 93 | ``` |
50 | 94 |
|
51 | | -## Project Setup Notes |
| 95 | +Note: The `application.properties` file has the connection information to connect to your Capella cluster. These will be part of the environment variables in the Docker container. |
| 96 | + |
| 97 | +### Verifying the Application |
| 98 | + |
| 99 | +Once the application starts, you can see the details of the application on the logs. |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +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](#swagger-documentation). |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## Running Tests |
| 108 | + |
| 109 | +To run the integration tests, use the following commands: |
| 110 | + |
| 111 | +```sh |
| 112 | +mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.AirlineIntegrationTest |
| 113 | +mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.AirportIntegrationTest |
| 114 | +mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.RouteIntegrationTest |
| 115 | + |
| 116 | +You can also run all the tests using the following command: |
| 117 | +mvn test |
| 118 | +``` |
| 119 | + |
| 120 | +## Appendix |
| 121 | + |
| 122 | +### Data Model |
| 123 | + |
| 124 | +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. |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +### Extending API by Adding New Entity |
| 129 | + |
| 130 | +If you would like to add another entity to the APIs, these are the steps to follow: |
| 131 | + |
| 132 | +- Create the new entity (collection) in the Couchbase bucket. You can create the collection using the [SDK](https://docs.couchbase.com/sdk-api/couchbase-java-client-3.5.2/com/couchbase/client/java/Collection.html#createScope-java.lang.String-) or via the [Couchbase Server interface](https://docs.couchbase.com/cloud/n1ql/n1ql-language-reference/createcollection.html). |
| 133 | +- Define the routes in a new file in the `controllers` folder similar to the existing routes like `AirportController.java`. |
| 134 | +- Define the service in a new file in the `services` folder similar to the existing services like `AirportService.java`. You'll have to implement the service interface `AirportServiceImpl.java`. |
| 135 | +- Define the repository in a new file in the `repositories` folder similar to the existing repositories like `AirportRepository.java`. You'll have to implement the repository interface `AirportRepositoryImpl.java`. |
| 136 | +- Add the new routes to the application in `Application.java`. |
| 137 | + |
| 138 | +### Running Self-Managed Couchbase Cluster |
| 139 | + |
| 140 | +If you are running this quickstart with a self-managed Couchbase cluster, you need to [load](https://docs.couchbase.com/server/current/manage/manage-settings/install-sample-buckets.html) the travel-sample data bucket in your cluster and generate the credentials for the bucket. |
| 141 | + |
| 142 | +You need to update the connection string and the credentials in the `application.properties` file in the `src/main/resources` folder. |
| 143 | + |
| 144 | +Note: Couchbase Server version 7 or higher must be installed and running before running the Spring Boot Java app. |
| 145 | + |
| 146 | +### Swagger Documentation |
| 147 | + |
| 148 | +Swagger documentation provides a clear view of the API including endpoints, HTTP methods, request parameters, and response objects. |
| 149 | + |
| 150 | +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. |
| 151 | + |
| 152 | +#### Trying Out the API |
| 153 | + |
| 154 | +You can try out an API by clicking on the "Try it out" button next to the endpoints. |
52 | 155 |
|
53 | | -This project was based on the standard [Spring Boot project](https://spring.io/guides/gs/rest-service/). The HealthCheckController is provided as a sanity check and is used in integration tests. |
| 156 | +- 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. |
54 | 157 |
|
55 | | -A full list of packages are referenced in the pom.xml file |
| 158 | +- 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. |
56 | 159 |
|
57 | | -## Conclusion |
| 160 | +#### Models |
58 | 161 |
|
59 | | -Setting up a basic REST API in Spring Boot with Couchbase is fairly simple. This project when run with Couchbase Server 7 installed creates a bucket in Couchbase, an index for our parameterized [N1QL query](https://docs.couchbase.com/java-sdk/current/howtos/n1ql-queries-with-sdk.html), and showcases basic CRUD operations needed in most applications. |
| 162 | +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. |
0 commit comments