You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Spring Boot and Spring Data Developer Portal Readmes (#24)
* added spring boot readme
* added spring data readme and updated springboot readme
* fixed the readmes based on the feedback + added images
* fixed the sring boot and spring data readmes based on feedback
* updated spring boot and spring data docs based on feedback
* moved the spring bott and spring data files into seperate folders
* updated the docs based on feedback
* changed to invalid capella string
* Updated Spring Boot and Spring Data documentation
* resolved comments based on feedback
* updated boot and data readmes
> Note: Maven packages auto restore when building the project in IntelliJ IDEA or Eclipse depending on IDE configuration.
54
+
> Note: Maven automatically restores packages when building the project. in IntelliJ IDEA or Eclipse depending on IDE configuration.
91
55
92
56
### Database Server Configuration
93
57
94
58
- The `CouchbaseConfig` class is a Spring configuration class responsible for setting up the connection to a Couchbase database in a Spring Boot application. It defines two beans:
95
59
96
60
-`getCouchbaseCluster()`: This bean creates and configures a connection to the Couchbase cluster using the provided hostname, username, and password.
97
61
98
-
-`getCouchbaseBucket(Cluster cluster)`: This bean creates a Couchbase bucket within the cluster if it doesn't already exist and returns the Bucket object associated with the specified bucket name.
62
+
-`getCouchbaseBucket(Cluster cluster)`: This bean retrieves a Couchbase bucket from a cluster, ensuring it exists and is ready within a timeout, throwing exceptions if not found or if connection times out.
99
63
100
64
### Application Properties
101
65
102
-
You need to configure the connection details to your Couchbase Server in the application.properties file located in the src/main/resources directory.
66
+
You need to configure the connection details to your Couchbase Server in the `application.properties` file located in the `src/main/resources` directory.
103
67
104
68
In the connection string, replace `DB_CONN_STR` with the connection string of your Couchbase cluster. Replace `DB_USERNAME` and `DB_PASSWORD` with the username and password of a Couchbase user with access to the bucket.
Note: The `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.
@@ -166,7 +130,7 @@ For this tutorial, we use three collections, `airport`, `airline` and `route` th
166
130
167
131
## Airline Document Structure
168
132
169
-
We will be setting up a REST API to manage some airline documents. The `name` 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 `country` field is the country of the airline.
133
+
We will be setting up a REST API to manage some airline documents. The `name` 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 `country` field is the country of the airline.
170
134
171
135
Our airline document will have a structure similar to the following example:
172
136
@@ -176,7 +140,7 @@ Our airline document will have a structure similar to the following example:
176
140
"callsign": "Couchbase",
177
141
"iata": "CB",
178
142
"icao": "CBA",
179
-
"country": "United States",
143
+
"country": "United States"
180
144
}
181
145
```
182
146
@@ -387,7 +351,7 @@ These workflows illustrate how each HTTP method interacts with the `AirlineServi
387
351
388
352
## Custom SQL++ Queries
389
353
390
-
1. Get all airlines by country
354
+
### 1. Get all airlines by country
391
355
392
356
```java
393
357
@@ -411,7 +375,7 @@ In the query, we are using the `country` parameter to filter the results by coun
411
375
412
376
Once the query is executed, the `AirlineController` constructs an HTTP response with a status code of 200 OK and includes the list of airlines in the response body as a list of JSON objects.
413
377
414
-
2. Get all airlines by destination airport
378
+
### 2. Get all airlines by destination airport
415
379
416
380
```java
417
381
@Override
@@ -437,12 +401,12 @@ Once the query is executed, the `AirlineController` constructs an HTTP response
437
401
438
402
## Running The Tests
439
403
404
+
This command will execute all the test cases in your project.
405
+
440
406
```sh
441
407
mvn test
442
408
```
443
409
444
-
This command will execute all the test cases in your project.
445
-
446
410
### Run Individual Tests:
447
411
448
412
Additionally, you can run individual test classes or methods using the following commands:
@@ -467,9 +431,9 @@ mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.RouteIntegration
467
431
468
432
## Project Setup Notes
469
433
470
-
This project was based on the standard [Spring Boot project](https://spring.io/guides/gs/rest-service/). The HealthCheckController is provided as a santity check and is used in unit tests.
434
+
This project was based on the standard [Spring Boot project](https://spring.io/guides/gs/rest-service/).
471
435
472
-
A full list of packages are referenced in the pom.xml file.
436
+
A full list of packages are referenced in the `pom.xml` file.
log.error("Error connecting to Couchbase cluster", e);
118
+
throw e;
119
+
}
120
+
}
121
+
122
+
@Bean
123
+
publicBucketgetCouchbaseBucket(Clustercluster) {
124
+
try {
125
+
if (!cluster.buckets().getAllBuckets().containsKey(getBucketName())) {
126
+
log.error("Bucket with name {} does not exist. Creating it now", getBucketName());
127
+
thrownewBucketNotFoundException(bucketName);
128
+
}
129
+
return cluster.bucket(getBucketName());
130
+
} catch (Exception e) {
131
+
log.error("Error getting bucket", e);
132
+
throw e;
133
+
}
142
134
}
143
135
144
-
...
136
+
}
145
137
```
146
138
147
139
> _from config/CouchbaseConfiguration.java_
148
140
141
+
These methods are used to configure and retrieve a Couchbase Cluster and a specific Bucket within that cluster in a Spring application.
142
+
143
+
-`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.
144
+
145
+
-`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.
146
+
149
147
This default configuration assumes that you have a locally running Couchbae server and uses standard administrative login and password for demonstration purpose.
150
148
Applications deployed to production or staging environments should use less privileged credentials created using [Role-Based Access Control](https://docs.couchbase.com/go-sdk/current/concept-docs/rbac.html).
151
149
Please refer to [Managing Connections using the Java SDK with Couchbase Server](https://docs.couchbase.com/java-sdk/current/howtos/managing-connections.html) for more information on Capella and local cluster connections.
@@ -229,11 +227,11 @@ We will be setting up a REST API to manage some airline documents. Our airline d
229
227
"callsign": "Couchbase",
230
228
"iata": "CB",
231
229
"icao": "CBA",
232
-
"country":"United States",
230
+
"country": "United States"
233
231
}
234
232
```
235
233
236
-
The `name` 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 `country` field is the country of the airline.
234
+
The `name` 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 `country` field is the country of the airline.
237
235
238
236
## Let's Review the Code
239
237
@@ -250,12 +248,20 @@ To begin clone the repo and open it up in the IDE of your choice to learn about
250
248
### Repository
251
249
252
250
`AirlineRepository.java`
253
-
This interface extends the `CouchbaseRepository` interface and provides methods for CRUD operations. The `@N1qlPrimaryIndexed` annotation creates a primary index on the `airline` collection. The `@ViewIndexed` annotation creates a view index on the `airline` collection. The `@Query` annotation 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.
251
+
This interface extends the `CouchbaseRepository` interface and provides methods for CRUD operations.
252
+
253
+
-`@Scope("..."):` Specifies the scope of the repository, which helps organize and manage documents within a Couchbase bucket.
254
+
255
+
-`@Collection("..."):` Specifies the collection within the bucket where the documents are stored.
256
+
257
+
-`@Repository:` Marks the interface as a repository component in the Spring application context.
258
+
259
+
-`@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)`: Specifies the scan consistency level for queries executed by methods in this repository, ensuring strong consistency for read operations.
254
260
255
261
### Model
256
262
257
263
`Airline.java`
258
-
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: `name`, `callsign`, `iata`, `icao`, `country`.
264
+
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: `name`, `callsign`, `iata`, `icao`, `country`.
0 commit comments