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
upgraded spring boot and swagger from 2 to 3, fixed readme and added images, changed javax to jakarta. Used environment variables instead of hardcoding
Instead of the hash symbols, you need to add the values for the Couchbase connection.
68
+
Instead of DB_CONN_STR, DB_USERNAME, and DB_PASSWORD, you should replace these with the connection string, username, and password for your Couchbase cluster. The connection string is the URL of your cluster. For example, if you are using Capella, the connection string will look like `couchbases://cb.jnym5s9gv4ealbe.cloud.couchbase.com`. If you are using a local cluster, the connection string will be `localhost`.
70
69
71
70
> Note: The connection string expects the `couchbases://` or `couchbase://` part.
72
71
73
72
74
73
## Cluster Connection Configuration
75
74
Spring Data couchbase connector can be configured by providing a `@Configuration`[bean](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition) that extends [`AbstractCouchbaseConfiguration`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.html).
76
-
The sample application provides a configuration bean that uses default couchbase login and password:
Thisdefault configuration assumes that you have a locally running Couchbae server and uses standard administrative login and password for demonstration purpose.
110
-
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).
111
-
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.
112
-
113
-
## Let's Review the Code
114
-
115
-
### Airline Model
116
-
117
-
The `Airline` model is a simple POJO (PlainOldJavaObject) that is used to represent the airline document in the travel-sample bucket. The `@Id` annotation is used to specify the document ID in the bucket. The `@Field` annotation is used to specify the field name in the document. The `@TypeAlias` annotation is used to specify the type of the document in the bucket.
118
-
119
-
```java
120
-
@Document
121
-
@Scope("inventory")
122
-
@Collection("airline")
123
-
public class Airline {
124
-
125
-
@Id
126
-
privateString id;
127
-
128
-
@Field("callsign")
129
-
privateString callsign;
130
-
131
-
@Field("country")
132
-
privateString country;
133
-
134
-
@Field("iata")
135
-
privateString iata;
136
-
137
-
@Field("icao")
138
-
privateString icao;
139
-
140
-
@Field("name")
141
-
privateString name;
142
-
143
-
@Field("type")
144
-
privateString type;
145
-
146
-
@Field("active")
147
-
privateboolean active;
148
-
149
-
...
150
-
```
151
-
>*from model/Airline.java*
152
-
153
-
The `@Document` annotation is used to specify that this classis a document in the bucket. The `@Scope` annotation is used to specify the scope of the document. The `@Collection` annotation is used to specify the collection of the document.
154
-
155
-
The `@Id` annotation is used to specify the document ID in the bucket. The `@Field` annotation is used to specify the field name in the document. The `@TypeAlias` annotation is used to specify the type of the document in the bucket.
156
-
157
-
You can find more information on key generation in the [Connector Documentation](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration).
158
-
159
-
Couchbase Spring Data connector will automatically serialize model instances into JSON when storing them on the cluster.
log.error("Error connecting to Couchbase cluster", e);
127
+
throw e;
128
+
}
129
+
}
162
130
163
-
The `Airline` document is stored in the `airline` collection in the `travel-sample` bucket. The document has the following structure:
131
+
@Bean
132
+
publicBucketgetCouchbaseBucket(Clustercluster) {
133
+
try {
134
+
if (!cluster.buckets().getAllBuckets().containsKey(getBucketName())) {
135
+
log.error("Bucket with name {} does not exist. Creating it now", getBucketName());
136
+
thrownewBucketNotFoundException(bucketName);
137
+
}
138
+
return cluster.bucket(getBucketName());
139
+
} catch (Exception e) {
140
+
log.error("Error getting bucket", e);
141
+
throw e;
142
+
}
143
+
}
164
144
165
-
```json
166
-
{
167
-
"callsign":"AMERICAN",
168
-
"country":"United States",
169
-
"iata":"AA",
170
-
"icao":"AAL",
171
-
"id":10,
172
-
"name":"American Airlines",
173
-
"type":"airline",
174
-
"active":true
175
145
}
176
146
```
147
+
> *from config/CouchbaseConfiguration.java*
177
148
149
+
This default configuration assumes that you have a locally running Couchbae server and uses standard administrative login and password for demonstration purpose.
150
+
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
+
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.
178
152
179
153
## Running The Application
180
154
@@ -212,11 +186,11 @@ Note: The `application.properties` file has the connection information to connec
212
186
213
187
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](#swagger-documentation).
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.
234
208
235
-

209
+

236
210
237
211
### Extending API by Adding New Entity
238
212
@@ -268,6 +242,3 @@ You can try out an API by clicking on the "Try it out" button next to the endpoi
268
242
#### Models
269
243
270
244
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.
271
-
272
-
## Conclusion
273
-
Setting up a basic REST API in Spring Data with Couchbase is fairly simple. This project, when run with Couchbase Server 7 installed creates a collection 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.
@OpenAPIDefinition(info = @Info(title = "Quickstart in Couchbase with Spring Data", version = "2.0", description = "<html><body><h2>A quickstart API using Java and Spring Data with Couchbase and travel-sample data</h2><p>We have a visual representation of the API documentation using Swagger which allows you to interact with the API's endpoints directly through the browser. It provides a clear view of the API including endpoints, HTTP methods, request parameters, and response objects.</p><p>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.</p><p><strong>Trying Out the API</strong></p><p>You can try out an API by clicking on the \"Try it out\" button next to the endpoints.</p><ul><li><strong>Parameters:</strong> 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.</li><li><strong>Execution:</strong> 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.</li></ul><p><strong>Models</strong></p><p>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.</p><p>For details on the API, please check the tutorial on the Couchbase Developer Portal: <a href=\"https://developer.couchbase.com/tutorial-quickstart-java-spring-boot\">https://developer.couchbase.com/tutorial-quickstart-java-spring-boot</a></p></body></html>"))
0 commit comments