We’ve done a little recoding. Introducing IBM Developer. The next level of coding, content, and community.Learn more
Finding patterns in data to derive information.
By Paul Bennett | Last updated September 20, 2018
The Cloudant NoSQL DB has a Spring Boot Starter to allow configuring Cloudant DB using Spring annotations and properties.
Cloudant NoSQL DB is a non-relational, distributed database based on the Apache CouchDB project and the open source BigCouch project.
This guide will show you how to use the Cloudant Spring Boot Starter to access a Cloudant DB service from a Spring Boot application. This example uses a Cloudant DB on IBM Cloud. You will perform the following steps:
On the Spring Initializr page generate a Maven Project with language Java, and the Web dependency. For this example we use group com.example and artifact cloudant-spring. Download the project and unzip it.
On the IBM Cloud Data & Analytics Catalog select the Cloudant NoSQL DB service. The Lite version of the service is available to try for free.
The Cloudant service can contain multiple databases, but initially has none.
To add databases select the Cloudant service on your IBM Cloud Dashboard and click on “Launch” to display the Cloudant Dashboard. On the dashboard select the “Databases” tab on the left and use the “Create Database” button on the top to add two databases.
Select the Cloudant service on your IBM Cloud Dashboard and then select “Service credentials” in the left pane. If no credentials are listed, click the “New credential” button to create a set of credentials. Then click on “View credentials” to display the service credentials.
Edit the unzipped Spring Initializr project and add the credentials to the src/main/resources/application.properties file with the following property names:
Note: It is not recommended to store credentials in your application. We do so here only for the sake of simplicity in this tutorial. The Cloudant Spring Boot Starter can utilize other property sources such as environment variables, etc.
For this example we’ll create a simple REST application with an endpoint that queries the Cloudant service and returns a list of the database names created in the previous step.
Edit the unzipped Spring Boot project to make the following changes:
Add the following dependency in the pom.xml dependencies section:
For the REST endpoint add a @RestController annotation to the Spring Boot application class that was created by the Spring Initializr – com/example/cloudantspring/CloudantSpringApplication.java. (Note that the Java package and class names are derived from the Group and Artifact values entered on the Initializr).
Your Spring Boot application should now look like the code below (shown here in the Intellij Idea IDE).
Build and run your app with the following command:
mvn package spring-boot:run
Now you can invoke the REST endpoint – http://localhost:8080/cloudant. You should see the message from your REST endpoint listing the DB’s in your Cloudant service:
The Cloudant Spring Boot Starter makes it easy to configure and use a Cloudant NoSQL DB in a Spring native fashion using Spring auto configuration and Spring Boot properties.
October 3, 2018
May 17, 2018
August 21, 2018
Back to top