Skip to content

Commit 14a89fd

Browse files
authored
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
1 parent c8a32d2 commit 14a89fd

File tree

2 files changed

+91
-121
lines changed

2 files changed

+91
-121
lines changed

tutorial/markdown/java/spring-boot/spring-boot.md

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# frontmatter
33
path: "/tutorial-quickstart-java-springboot"
44
# title and description do not need to be added to markdown, start with H2 (##)
5-
title: Start with Java and Spring Boot
5+
title: Quickstart in Couchbase with Java and Spring Boot
66
short_title: Java and Spring Boot
77
description:
8-
- Learn how to configure Spring Data with Couchbase
8+
- Learn to build a REST API in Java using Spring Boot and Couchbase
99
- Explore key-based operations and SQL++ querying using Spring Data Couchbase repositories
10-
- Build a simple REST APIs that stores user profiles on a Couchbase cluster
10+
- Explore CRUD operations in action with Couchbase
1111
content_type: quickstart
1212
filter: sdk
1313
technology:
@@ -27,42 +27,6 @@ length: 30 Mins
2727

2828
<!-- TODO: Figure out how to add width to image size in try it now links -->
2929

30-
## Table of Contents
31-
32-
- [Getting Started](#getting-started)
33-
- [Prerequisites](#prerequisites)
34-
- [Source Code](#source-code)
35-
- [Install Dependencies](#install-dependencies)
36-
- [Database Server Configuration](#database-server-configuration)
37-
- [Application Properties](#application-properties)
38-
- [Running The Application](#running-the-application)
39-
- [Directly on the machine](#directly-on-the-machine)
40-
- [Docker](#docker)
41-
- [Data Model](#data-model)
42-
- [Airline Document Structure](#airline-document-structure)
43-
- [Let's Review the Code](#lets-review-the-code)
44-
- [Code Organization](#code-organization)
45-
- [Model](#model)
46-
- [Controller](#controller)
47-
- [Service](#service)
48-
- [Repository](#repository)
49-
- [Mapping Workflow](#mapping-workflow)
50-
- [GET Mapping Workflow](#get-mapping-workflow)
51-
- [POST Mapping Workflow](#post-mapping-workflow)
52-
- [PUT Mapping Workflow](#put-mapping-workflow)
53-
- [DELETE Mapping Workflow](#delete-mapping-workflow)
54-
- [Custom SQL++ Queries](#custom-sql-queries)
55-
- [Get all airlines by country](#get-all-airlines-by-country)
56-
- [Get all airlines by destination airport](#get-all-airlines-by-destination-airport)
57-
- [Running The Tests](#running-the-tests)
58-
- [Run Individual Tests](#run-individual-tests)
59-
- [Project Setup Notes](#project-setup-notes)
60-
- [Contributing](#contributing)
61-
- [Appendix](#appendix)
62-
- [Extending API by Adding New Entity](#extending-api-by-adding-new-entity)
63-
- [Running Self Managed Couchbase Cluster](#running-self-managed-couchbase-cluster)
64-
- [Swagger Documentation](#swagger-documentation)
65-
6630
## Getting Started
6731

6832
### Prerequisites
@@ -87,19 +51,19 @@ git clone https://github.com/couchbase-examples/java-springboot-quickstart.git
8751
mvn package
8852
```
8953

90-
> 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.
9155
9256
### Database Server Configuration
9357

9458
- 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:
9559

9660
- `getCouchbaseCluster()`: This bean creates and configures a connection to the Couchbase cluster using the provided hostname, username, and password.
9761

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.
9963

10064
### Application Properties
10165

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.
10367

10468
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.
10569

@@ -144,7 +108,7 @@ docker build -t java-springboot-quickstart .
144108
Run the Docker image
145109

146110
```sh
147-
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>
111+
docker run -d --name springboot-container -p 8080:8080 java-springboot-quickstart -e DB_CONN_STR=<connection_string> -e DB_USERNAME=<username> -e DB_PASSWORD=<password>
148112
```
149113

150114
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
166130

167131
## Airline Document Structure
168132

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.
170134

171135
Our airline document will have a structure similar to the following example:
172136

@@ -176,7 +140,7 @@ Our airline document will have a structure similar to the following example:
176140
"callsign": "Couchbase",
177141
"iata": "CB",
178142
"icao": "CBA",
179-
"country": "United States",
143+
"country": "United States"
180144
}
181145
```
182146

@@ -387,7 +351,7 @@ These workflows illustrate how each HTTP method interacts with the `AirlineServi
387351

388352
## Custom SQL++ Queries
389353

390-
1. Get all airlines by country
354+
### 1. Get all airlines by country
391355

392356
```java
393357

@@ -411,7 +375,7 @@ In the query, we are using the `country` parameter to filter the results by coun
411375

412376
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.
413377

414-
2. Get all airlines by destination airport
378+
### 2. Get all airlines by destination airport
415379

416380
```java
417381
@Override
@@ -437,12 +401,12 @@ Once the query is executed, the `AirlineController` constructs an HTTP response
437401

438402
## Running The Tests
439403

404+
This command will execute all the test cases in your project.
405+
440406
```sh
441407
mvn test
442408
```
443409

444-
This command will execute all the test cases in your project.
445-
446410
### Run Individual Tests:
447411

448412
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
467431

468432
## Project Setup Notes
469433

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/).
471435

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.
473437

474438
## Contributing
475439

tutorial/markdown/java/spring-data/spring-data.md

Lines changed: 76 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,25 @@
11
---
22
# frontmatter
33
path: "/tutorial-quickstart-java-springdata"
4-
title: Couchbase With Spring-Boot and Spring Data
5-
short_title: Spring Data
4+
title: Quickstart in Couchbase with Java and Spring Data
5+
short_title: Java and Spring Data
66
description:
7-
- Build a REST API with Couchbase and Spring Data
8-
- Learn how to configure the Couchbase SDK
9-
content_type: tutorial
7+
- Learn to build a REST API in Java using Spring Data and Couchbase
8+
- Explore key-based operations and SQL++ querying using Spring Data Couchbase repositories
9+
- Explore CRUD operations in action with Couchbase
10+
content_type: quickstart
1011
filter: sdk
1112
technology:
1213
- kv
1314
- query
1415
tags:
16+
- REST API
1517
- Spring Data
1618
sdk_language:
1719
- java
1820
length: 30 Mins
1921
---
2022

21-
## Table of Contents
22-
23-
- [Getting Started](#getting-started)
24-
- [Prerequisites](#prerequisites)
25-
- [Source Code](#source-code)
26-
- [Install Dependencies](#install-dependencies)
27-
- [Database Server Configuration](#database-server-configuration)
28-
- [Application Properties](#application-properties)
29-
- [Running the Application](#running-the-application)
30-
- [Directly on Machine](#directly-on-machine)
31-
- [Using Docker](#using-docker)
32-
- [Data Model](#data-model)
33-
- [Airline Document Structure](#airline-document-structure)
34-
- [Let's Review the Code](#lets-review-the-code)
35-
- [Code Organization](#code-organization)
36-
- [Repository](#repository)
37-
- [Model](#model)
38-
- [Controller](#controller)
39-
- [Service](#service)
40-
- [Mapping Workflow](#mapping-workflow)
41-
- [GET Mapping Workflow](#get-mapping-workflow)
42-
- [POST Mapping Workflow](#post-mapping-workflow)
43-
- [PUT Mapping Workflow](#put-mapping-workflow)
44-
- [DELETE Mapping Workflow](#delete-mapping-workflow)
45-
- [Custom SQL++ Queries](#custom-sql-queries)
46-
- [Running The Tests](#running-the-tests)
47-
- [Project Setup Notes](#project-setup-notes)
48-
- [Contributing](#contributing)
49-
- [Appendix](#appendix)
50-
- [Extending API by Adding New Entity](#extending-api-by-adding-new-entity)
51-
- [Running Self Managed Couchbase Cluster](#running-self-managed-couchbase-cluster)
52-
- [Swagger Documentation](#swagger-documentation)
53-
5423
## Getting Started
5524

5625
### Prerequisites
@@ -77,32 +46,14 @@ git clone https://github.com/couchbase-examples/java-springdata-quickstart.git
7746

7847
Gradle dependencies:
7948

80-
```groovy
49+
```gradle
8150
implementation 'org.springframework.boot:spring-boot-starter-web'
8251
// spring data couchbase connector
8352
implementation 'org.springframework.boot:spring-boot-starter-data-couchbase'
8453
// swagger ui
8554
implementation 'org.springdoc:springdoc-openapi-ui:1.6.6'
8655
```
8756

88-
Maven dependencies:
89-
90-
```xml
91-
<dependency>
92-
<groupId>org.springframework.boot</groupId>
93-
<artifactId>spring-boot-starter-data-couchbase</artifactId>
94-
</dependency>
95-
<dependency>
96-
<groupId>org.springframework.boot</groupId>
97-
<artifactId>spring-boot-starter-web</artifactId>
98-
</dependency>
99-
<dependency>
100-
<groupId>org.springdoc</groupId>
101-
<artifactId>springdoc-openapi-ui</artifactId>
102-
<version>1.6.6</version>
103-
</dependency>
104-
```
105-
10657
#### Useful Links
10758

10859
- [Spring Data Couchbase - Reference Documentation](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/)
@@ -114,38 +65,85 @@ Spring Data couchbase connector can be configured by providing a `@Configuration
11465
The sample application provides a configuration bean that uses default couchbase login and password:
11566

11667
```java
68+
@Slf4j
11769
@Configuration
70+
@EnableCouchbaseRepositories
11871
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
11972

73+
@Value("#{systemEnvironment['DB_CONN_STR'] ?: '${spring.couchbase.bootstrap-hosts:localhost}'}")
74+
private String host;
75+
76+
@Value("#{systemEnvironment['DB_USERNAME'] ?: '${spring.couchbase.bucket.user:Administrator}'}")
77+
private String username;
78+
79+
@Value("#{systemEnvironment['DB_PASSWORD'] ?: '${spring.couchbase.bucket.password:password}'}")
80+
private String password;
81+
82+
@Value("${spring.couchbase.bucket.name:travel-sample}")
83+
private String bucketName;
84+
12085
@Override
12186
public String getConnectionString() {
122-
// capella
123-
// return "couchbases://cb.xyz.cloud.couchbase.com";
124-
125-
// localhost
126-
return "127.0.0.1"
87+
return host;
12788
}
12889

12990
@Override
13091
public String getUserName() {
131-
return "Administrator";
92+
return username;
13293
}
13394

13495
@Override
13596
public String getPassword() {
136-
return "password";
97+
return password;
13798
}
13899

139100
@Override
140101
public String getBucketName() {
141-
return "springdata_quickstart";
102+
return bucketName;
103+
}
104+
105+
@Override
106+
public String typeKey() {
107+
return "type";
108+
}
109+
110+
@Override
111+
@Bean(destroyMethod = "disconnect")
112+
public Cluster couchbaseCluster(ClusterEnvironment couchbaseClusterEnvironment) {
113+
try {
114+
log.debug("Connecting to Couchbase cluster at " + host);
115+
return Cluster.connect(getConnectionString(), getUserName(), getPassword());
116+
} catch (Exception e) {
117+
log.error("Error connecting to Couchbase cluster", e);
118+
throw e;
119+
}
120+
}
121+
122+
@Bean
123+
public Bucket getCouchbaseBucket(Cluster cluster) {
124+
try {
125+
if (!cluster.buckets().getAllBuckets().containsKey(getBucketName())) {
126+
log.error("Bucket with name {} does not exist. Creating it now", getBucketName());
127+
throw new BucketNotFoundException(bucketName);
128+
}
129+
return cluster.bucket(getBucketName());
130+
} catch (Exception e) {
131+
log.error("Error getting bucket", e);
132+
throw e;
133+
}
142134
}
143135

144-
...
136+
}
145137
```
146138

147139
> _from config/CouchbaseConfiguration.java_
148140
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+
149147
This default configuration assumes that you have a locally running Couchbae server and uses standard administrative login and password for demonstration purpose.
150148
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).
151149
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
229227
"callsign": "Couchbase",
230228
"iata": "CB",
231229
"icao": "CBA",
232-
"country": "United States",
230+
"country": "United States"
233231
}
234232
```
235233

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.
237235

238236
## Let's Review the Code
239237

@@ -250,12 +248,20 @@ To begin clone the repo and open it up in the IDE of your choice to learn about
250248
### Repository
251249

252250
`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.
254260

255261
### Model
256262

257263
`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`.
259265

260266
### Controller
261267

0 commit comments

Comments
 (0)