Persisting data with MongoDB

duration 25 minutes
New

Prerequisites:

Learn how to persist data in your microservices to MongoDB, a document-oriented NoSQL database.

What you’ll learn

You will learn how to use MongoDB to build and test a simple microservice that manages the members of a crew. The microservice will respond to POST, GET, PUT, and DELETE requests that manipulate the database.

The crew members will be stored in MongoDB as documents in the following JSON format:

{
  "_id": {
    "$oid": "5dee6b079503234323db2ebc"
  },
  "Name": "Member1",
  "Rank": "Captain",
  "CrewID": "000001"
}

This microservice connects to MongoDB by using Transport Layer Security (TLS) and injects a MongoDatabase instance into the service with a Contexts and Dependency Injection (CDI) producer. Additionally, MicroProfile Config is used to easily configure the MongoDB driver.

For more information about CDI and MicroProfile Config, see the guides on Injecting dependencies into microservices and Separating configuration from code in microservices.

Additional prerequisites

You will use Docker to run an instance of MongoDB for a fast installation and setup. Install Docker by following the instructions in the official Docker documentation, and start your Docker environment.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone https://github.com/openliberty/guide-mongodb-intro.git
cd guide-mongodb-intro

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project that you will build.

Before you begin, make sure you have all the necessary prerequisites.

Setting up MongoDB

This guide uses Docker to run an instance of MongoDB. A multi-stage Dockerfile is provided for you. This Dockerfile uses the mongo image as the base image of the final stage and gathers the required configuration files. The resulting mongo image runs in a Docker container, and you must set up a new database for the microservice. Lastly, the truststore that’s generated in the Docker image is copied from the container and placed into the Open Liberty server.

You can find more details and configuration options on the MongoDB website. For more information about the mongo image, see mongo in Docker Hub.

Running MongoDB in a Docker container

Run the following commands to use the Dockerfile to build the image, run the image in a Docker container, and map port 27017 from the container to your host machine:

docker build -t mongo-sample -f assets/Dockerfile .
docker run --name mongo-guide -p 27017:27017 -d mongo-sample

Adding the truststore to the Open Liberty server

The truststore that’s created in the container needs to be added to the Open Liberty server so that the server can trust the certificate that MongoDB presents when they connect. Run the following command to copy the truststore.p12 file from the container to the start and finish directories:

docker cp ^
  mongo-guide:/home/mongodb/certs/truststore.p12 ^
  start/src/main/liberty/config/resources/security
docker cp ^
  mongo-guide:/home/mongodb/certs/truststore.p12 ^
  finish/src/main/liberty/config/resources/security
docker cp \
  mongo-guide:/home/mongodb/certs/truststore.p12 \
  start/src/main/liberty/config/resources/security
docker cp \
  mongo-guide:/home/mongodb/certs/truststore.p12 \
  finish/src/main/liberty/config/resources/security
docker cp \
  mongo-guide:/home/mongodb/certs/truststore.p12 \
  start/src/main/liberty/config/resources/security
docker cp \
  mongo-guide:/home/mongodb/certs/truststore.p12 \
  finish/src/main/liberty/config/resources/security

Try what you’ll build

The finish directory in the root of this guide contains the finished application. Give it a try before you proceed.

To try out the application, first go to the finish directory and run the following Maven goal to build the application and deploy it to Open Liberty:

cd finish
mvn liberty:run

After you see the following message, your application server is ready:

The defaultServer server is ready to run a smarter planet.

You can now check out the service by going to the http://localhost:9080/mongo/ URL.

After you are finished checking out the application, stop the Open Liberty server by pressing CTRL+C in the command-line session where you ran the server. Alternatively, you can run the liberty:stop goal from the finish directory in another shell session:

mvn liberty:stop

Providing a MongoDatabase

Navigate to the start directory to begin.

When you run Open Liberty in development mode, known as dev mode, the server listens for file changes and automatically recompiles and deploys your updates whenever you save a new change. Run the following goal to start Open Liberty in dev mode:

mvn liberty:dev

After you see the following message, your application server in dev mode is ready:

**************************************************************
*    Liberty is running in dev mode.

Dev mode holds your command-line session to listen for file changes. Open another command-line session to continue, or open the project in your editor.

With a CDI producer, you can easily provide a MongoDatabase to your microservice.

Create the MongoProducer class.
src/main/java/io/openliberty/guides/mongo/MongoProducer.java

MongoProducer.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2020 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License v1.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-v10.html
  8 *
  9 * Contributors:
 10 *     IBM Corporation - initial API and implementation
 11 *******************************************************************************/
 12// end::copyright[]
 13package io.openliberty.guides.mongo;
 14
 15import javax.enterprise.context.ApplicationScoped;
 16import javax.enterprise.inject.Disposes;
 17import javax.enterprise.inject.Produces;
 18import javax.inject.Inject;
 19import javax.net.ssl.SSLContext;
 20
 21import com.ibm.websphere.ssl.JSSEHelper;
 22import com.ibm.websphere.ssl.SSLException;
 23import org.eclipse.microprofile.config.inject.ConfigProperty;
 24
 25import com.ibm.websphere.crypto.PasswordUtil;
 26import com.mongodb.MongoClient;
 27import com.mongodb.MongoClientOptions;
 28import com.mongodb.MongoCredential;
 29import com.mongodb.ServerAddress;
 30import com.mongodb.client.MongoDatabase;
 31
 32import java.util.Collections;
 33
 34@ApplicationScoped
 35public class MongoProducer {
 36
 37    // tag::mongoProducerInjections[]
 38    @Inject
 39    @ConfigProperty(name = "mongo.hostname", defaultValue = "localhost")
 40    String hostname;
 41
 42    @Inject
 43    @ConfigProperty(name = "mongo.port", defaultValue = "27017")
 44    int port;
 45
 46    @Inject
 47    @ConfigProperty(name = "mongo.dbname", defaultValue = "testdb")
 48    String dbName;
 49
 50    @Inject
 51    @ConfigProperty(name = "mongo.user")
 52    String user;
 53
 54    @Inject
 55    @ConfigProperty(name = "mongo.pass.encoded")
 56    String encodedPass;
 57    // end::mongoProducerInjections[]
 58
 59    // tag::produces1[]
 60    @Produces
 61    // end::produces1[]
 62    // tag::createMongo[]
 63    public MongoClient createMongo() throws SSLException {
 64        // tag::decode[]
 65        String password = PasswordUtil.passwordDecode(encodedPass);
 66        // end::decode[]
 67        // tag::createCredential[]
 68        MongoCredential creds = MongoCredential.createCredential(
 69                user,
 70                dbName,
 71                password.toCharArray()
 72        );
 73        // end::createCredential[]
 74
 75        // tag::sslContext[]
 76        SSLContext sslContext = JSSEHelper.getInstance().getSSLContext(
 77                // tag::outboundSSLContext[]
 78                "outboundSSLContext",
 79                // end::outboundSSLContext[]
 80                Collections.emptyMap(),
 81                null
 82        );
 83        // end::sslContext[]
 84
 85        // tag::mongoClient[]
 86        return new MongoClient(
 87                // tag::serverAddress[] 
 88                new ServerAddress(hostname, port),
 89                // end::serverAddress[]
 90                // tag::creds[]
 91                creds,
 92                // end::creds[]
 93                // tag::optionsBuilder[]
 94                new MongoClientOptions.Builder()
 95                        .sslEnabled(true)
 96                        .sslContext(sslContext)
 97                        .build()
 98                // end::optionsBuilder[]
 99        );
100        // end::mongoClient[]
101    }
102    // end::createMongo[]
103
104    // tag::produces2[]
105    @Produces
106    // end::produces2[]
107    // tag::createDB[]
108    public MongoDatabase createDB(
109            // tag::injectMongoClient[]
110            MongoClient client) {
111            // end::injectMongoClient[]
112        // tag::getDatabase[]
113        return client.getDatabase(dbName);
114        // end::getDatabase[]
115    }
116    // end::createDB[]
117
118    // tag::close[]
119    public void close(
120            // tag::disposes[]
121            @Disposes MongoClient toClose) {
122            // end::disposes[]
123        // tag::toClose[]
124        toClose.close();
125        // end::toClose[]
126    }
127    // end::close[]
128}

microprofile-config.properties

 1# tag::hostname[]
 2mongo.hostname=localhost
 3# end::hostname[]
 4# tag::port[]
 5mongo.port=27017
 6# end::port[]
 7# tag::dbname[]
 8mongo.dbname=testdb
 9# end::dbname[]
10# tag::mongoUser[]
11mongo.user=sampleUser
12# end::mongoUser[]
13# tag::passEncoded[]
14mongo.pass.encoded={aes}APtt+/vYxxPa0jE1rhmZue9wBm3JGqFK3JR4oJdSDGWM1wLr1ckvqkqKjSB2Voty8g==
15# tag::passEncoded[]

pom.xml

  1<?xml version="1.0" encoding="UTF-8"?>
  2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3
  4    <modelVersion>4.0.0</modelVersion>
  5
  6    <groupId>io.openliberty.guides</groupId>
  7    <artifactId>guide-mongodb-intro</artifactId>
  8    <version>1.0-SNAPSHOT</version>
  9    <packaging>war</packaging>
 10
 11    <properties>
 12        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 13        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 14        <maven.compiler.source>1.8</maven.compiler.source>
 15        <maven.compiler.target>1.8</maven.compiler.target>
 16        <!-- OpenLiberty runtime -->
 17        <version.openliberty-runtime>RELEASE</version.openliberty-runtime>
 18        <!-- tag::defaultHttpPort[] -->
 19        <liberty.var.default.http.port>9080</liberty.var.default.http.port>
 20        <!-- end::defaultHttpPort[] -->
 21        <!-- tag::defaultHttpsPort[] -->
 22        <liberty.var.default.https.port>9443</liberty.var.default.https.port>
 23        <!-- end::defaultHttpsPort[] -->
 24        <app.name>${project.artifactId}</app.name>
 25        <!-- tag::appContextRoot[] -->
 26        <liberty.var.app.context.root>/mongo</liberty.var.app.context.root>
 27        <!-- end::appContextRoot[] -->
 28        <package.file>${project.build.directory}/${app.name}.zip</package.file>
 29    </properties>
 30
 31    <dependencies>
 32        <!-- Provided dependencies -->
 33        <dependency>
 34            <groupId>jakarta.platform</groupId>
 35            <artifactId>jakarta.jakartaee-api</artifactId>
 36            <version>8.0.0</version>
 37            <scope>provided</scope>
 38        </dependency>
 39        <dependency>
 40            <groupId>org.eclipse.microprofile</groupId>
 41            <artifactId>microprofile</artifactId>
 42            <version>4.0.1</version>
 43            <type>pom</type>
 44            <scope>provided</scope>
 45        </dependency>
 46        <dependency>
 47            <groupId>javax.validation</groupId>
 48            <artifactId>validation-api</artifactId>
 49            <version>2.0.1.Final</version>
 50            <scope>provided</scope>
 51        </dependency>
 52        <!-- tag::passwordUtilDependency[] -->
 53        <dependency>
 54            <groupId>com.ibm.websphere.appserver.api</groupId>
 55            <artifactId>com.ibm.websphere.appserver.api.passwordUtil</artifactId>
 56            <version>1.0.51</version>
 57            <scope>provided</scope>
 58        </dependency>
 59        <!-- end::passwordUtilDependency[] -->
 60        <!-- tag::sslDependency[] -->
 61        <dependency>
 62            <groupId>com.ibm.websphere.appserver.api</groupId>
 63            <artifactId>com.ibm.websphere.appserver.api.ssl</artifactId>
 64            <version>1.4.51</version>
 65            <scope>provided</scope>
 66        </dependency>
 67        <!-- end::sslDependency[] -->
 68        <!-- Required dependencies -->
 69        <!-- tag::mongoDriver[] -->
 70        <dependency>
 71            <groupId>org.mongodb</groupId>
 72            <artifactId>mongo-java-driver</artifactId>
 73            <version>3.12.10</version>
 74        </dependency>
 75        <!-- end::mongoDriver[] -->
 76        <!-- For tests -->
 77        <dependency>
 78            <groupId>org.apache.cxf</groupId>
 79            <artifactId>cxf-rt-rs-client</artifactId>
 80            <version>3.4.3</version>
 81            <scope>test</scope>
 82        </dependency>
 83        <dependency>
 84            <groupId>org.apache.cxf</groupId>
 85            <artifactId>cxf-rt-rs-extension-providers</artifactId>
 86            <version>3.4.3</version>
 87            <scope>test</scope>
 88        </dependency>
 89        <dependency>
 90            <groupId>org.glassfish</groupId>
 91            <artifactId>javax.json</artifactId>
 92            <version>1.1.4</version>
 93            <scope>test</scope>
 94        </dependency>
 95        <dependency>
 96            <groupId>org.junit.jupiter</groupId>
 97            <artifactId>junit-jupiter</artifactId>
 98            <version>5.7.1</version>
 99            <scope>test</scope>
100        </dependency>
101        <dependency>
102            <groupId>javax.xml.bind</groupId>
103            <artifactId>jaxb-api</artifactId>
104            <version>2.3.1</version>
105            <scope>test</scope>
106        </dependency>
107    </dependencies>
108
109    <build>
110        <defaultGoal>clean package liberty:run-server</defaultGoal>
111        <finalName>${project.artifactId}</finalName>
112        <plugins>
113            <plugin>
114                <groupId>org.apache.maven.plugins</groupId>
115                <artifactId>maven-war-plugin</artifactId>
116                <version>3.3.1</version>
117            </plugin>
118            <!-- Enable liberty-maven plugin -->
119            <plugin>
120                <groupId>io.openliberty.tools</groupId>
121                <artifactId>liberty-maven-plugin</artifactId>
122                <version>3.4</version>
123            </plugin>
124            <!-- Plugin to run unit tests -->
125            <plugin>
126                <groupId>org.apache.maven.plugins</groupId>
127                <artifactId>maven-surefire-plugin</artifactId>
128                <version>2.22.2</version>
129            </plugin>
130            <!-- Plugin to run functional tests -->
131            <plugin>
132                <groupId>org.apache.maven.plugins</groupId>
133                <artifactId>maven-failsafe-plugin</artifactId>
134                <version>2.22.2</version>
135                <configuration>
136                    <!-- tag::testsysprops[] -->
137                    <systemPropertyVariables>
138                        <app.http.port>${liberty.var.default.http.port}</app.http.port>
139                        <app.context.root>${liberty.var.app.context.root}</app.context.root>
140                    </systemPropertyVariables>
141                    <!-- end::testsysprops[] -->
142                </configuration>
143            </plugin>
144        </plugins>
145    </build>
146</project>

server.xml

 1<server description="Sample Liberty server">
 2    <!-- tag::featureManager[] -->
 3    <featureManager>
 4        <!-- tag::cdiFeature[] -->
 5        <feature>cdi-2.0</feature>
 6        <!-- end::cdiFeature[] -->
 7        <!-- tag::sslFeature[] -->
 8        <feature>ssl-1.0</feature>
 9        <!-- end::sslFeature[] -->
10        <!-- tag::mpConfigFeature[] -->
11        <feature>mpConfig-2.0</feature>
12        <!-- end::mpConfigFeature[] -->
13        <!-- tag::passwordUtilFeature[] -->
14        <feature>passwordUtilities-1.0</feature>
15        <!-- end::passwordUtilFeature[] -->
16        <feature>beanValidation-2.0</feature>           
17        <feature>jaxrs-2.1</feature>
18        <feature>mpOpenAPI-2.0</feature>
19    </featureManager>
20    <!-- end::featureManager[] -->
21
22    <variable name="default.http.port" defaultValue="9080"/>
23    <variable name="default.https.port" defaultValue="9443"/>
24    <variable name="app.context.root" defaultValue="/mongo"/>
25
26    <!-- tag::httpEndpoint[] -->
27    <httpEndpoint
28        host="*" 
29        httpPort="${default.http.port}" 
30        httpsPort="${default.https.port}" 
31        id="defaultHttpEndpoint"
32    />
33    <!-- end::httpEndpoint[] -->
34
35    <!-- tag::webApplication[] -->
36    <webApplication 
37        location="guide-mongodb-intro.war" 
38        contextRoot="${app.context.root}"
39    />
40    <!-- end::webApplication[] -->
41    <!-- tag::sslContext[] -->
42    <!-- tag::keyStore[] -->
43    <keyStore
44        id="outboundTrustStore" 
45        location="${server.output.dir}/resources/security/truststore.p12"
46        password="mongodb"
47        type="PKCS12" 
48    />
49    <!-- end::keyStore[] -->
50    <!-- tag::ssl[] -->
51    <ssl 
52        id="outboundSSLContext" 
53        keyStoreRef="defaultKeyStore" 
54        trustStoreRef="outboundTrustStore" 
55        sslProtocol="TLS" 
56    />
57    <!-- end::ssl[] -->
58    <!-- end::sslContext[] -->
59</server>

The values from the microprofile-config.properties file are injected into the MongoProducer class. The MongoProducer class requires the following methods for the MongoClient:

  • The createMongo() producer method returns an instance of MongoClient. In this method, the username, database name, and decoded password are passed into the MongoCredential.createCredential() method to get an instance of MongoCredential. The JSSEHelper gets the SSLContext from the outboundSSLContext in the server.xml file. Then, a MongoClient instance is created.

  • The createDB() producer method returns an instance of MongoDatabase that depends on the MongoClient. This method injects the MongoClient in its parameters and passes the database name into the MongoClient.getDatabase() method to get a MongoDatabase instance.

  • The close() method is a clean-up function for the MongoClient that closes the connection to the MongoDatabase instance.

Implementing the Create, Retrieve, Update, and Delete operations

You are going to implement the basic create, retrieve, update, and delete (CRUD) operations in the CrewService class. The com.mongodb.client and com.mongodb.client.result packages are used to help implement these operations for the microservice. For more information about these packages, see the com.mongodb.client and com.mongodb.client.result Javadoc. For more information about creating a RESTful service with JAX-RS, JSON-B, and Open Liberty, see the guide on Creating a RESTful web serivce.

Create the CrewService class.
src/main/java/io/openliberty/guides/application/CrewService.java

CrewService.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2020 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License v1.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-v10.html
  8 *
  9 * Contributors:
 10 *     IBM Corporation - initial API and implementation
 11 *******************************************************************************/
 12// end::copyright[]
 13package io.openliberty.guides.application;
 14
 15import java.util.Set;
 16
 17import java.io.StringWriter;
 18
 19import javax.enterprise.context.ApplicationScoped;
 20import javax.inject.Inject;
 21import javax.json.JsonArray;
 22import javax.json.JsonArrayBuilder;
 23import javax.json.Json;
 24import javax.ws.rs.GET;
 25import javax.ws.rs.POST;
 26import javax.ws.rs.PUT;
 27import javax.ws.rs.Path;
 28import javax.ws.rs.Consumes;
 29import javax.ws.rs.DELETE;
 30import javax.ws.rs.Produces;
 31import javax.ws.rs.PathParam;
 32import javax.ws.rs.core.MediaType;
 33import javax.ws.rs.core.Response;
 34
 35import javax.validation.Validator;
 36import javax.validation.ConstraintViolation;
 37
 38import com.mongodb.client.FindIterable;
 39// tag::bsonDocument[]
 40import org.bson.Document;
 41// end::bsonDocument[]
 42import org.bson.types.ObjectId;
 43
 44// tag::mongoImports1[]
 45import com.mongodb.client.MongoCollection;
 46import com.mongodb.client.MongoDatabase;
 47// end::mongoImports1[]
 48// tag::mongoImports2[]
 49import com.mongodb.client.result.DeleteResult;
 50import com.mongodb.client.result.UpdateResult;
 51// end::mongoImports2[]
 52
 53import org.eclipse.microprofile.openapi.annotations.Operation;
 54import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
 55import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
 56import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
 57
 58@Path("/crew")
 59@ApplicationScoped
 60public class CrewService {
 61
 62    // tag::dbInjection[]
 63    @Inject
 64    MongoDatabase db;
 65    // end::dbInjection[]
 66
 67    // tag::beanValidator[]
 68    @Inject
 69    Validator validator;
 70    // end::beanValidator[]
 71
 72    // tag::getViolations[]
 73    private JsonArray getViolations(CrewMember crewMember) {
 74        Set<ConstraintViolation<CrewMember>> violations = validator.validate(
 75                crewMember);
 76
 77        JsonArrayBuilder messages = Json.createArrayBuilder();
 78
 79        for (ConstraintViolation<CrewMember> v : violations) {
 80            messages.add(v.getMessage());
 81        }
 82
 83        return messages.build();
 84    }
 85    // end::getViolations[]
 86
 87    @POST
 88    @Path("/")
 89    @Consumes(MediaType.APPLICATION_JSON)
 90    @Produces(MediaType.APPLICATION_JSON)
 91    @APIResponses({
 92        @APIResponse(
 93            responseCode = "200",
 94            description = "Successfully added crew member."),
 95        @APIResponse(
 96            responseCode = "400",
 97            description = "Invalid crew member configuration.") })
 98    @Operation(summary = "Add a new crew member to the database.")
 99    // tag::add[]
100    public Response add(CrewMember crewMember) {
101        JsonArray violations = getViolations(crewMember);
102
103        if (!violations.isEmpty()) {
104            return Response
105                    .status(Response.Status.BAD_REQUEST)
106                    .entity(violations.toString())
107                    .build();
108        }
109
110        // tag::getCollection[]
111        MongoCollection<Document> crew = db.getCollection("Crew");
112        // end::getCollection[]
113
114        // tag::crewMemberCreation[]
115        Document newCrewMember = new Document();
116        newCrewMember.put("Name", crewMember.getName());
117        newCrewMember.put("Rank", crewMember.getRank());
118        newCrewMember.put("CrewID", crewMember.getCrewID());
119        // end::crewMemberCreation[]
120
121        // tag::insertOne[]
122        crew.insertOne(newCrewMember);
123        // end::insertOne[]
124        
125        return Response
126            .status(Response.Status.OK)
127            .entity(newCrewMember.toJson())
128            .build();
129    }
130    // end::add[]
131
132    @GET
133    @Path("/")
134    @Produces(MediaType.APPLICATION_JSON)
135    @APIResponses({
136        @APIResponse(
137            responseCode = "200",
138            description = "Successfully listed the crew members."),
139        @APIResponse(
140            responseCode = "500",
141            description = "Failed to list the crew members.") })
142    @Operation(summary = "List the crew members from the database.")
143    // tag::retrieve[]
144    public Response retrieve() {
145        StringWriter sb = new StringWriter();
146
147        try {
148            // tag::getCollectionRead[]
149            MongoCollection<Document> crew = db.getCollection("Crew");
150            // end::getCollectionRead[]
151            sb.append("[");
152            boolean first = true;
153            // tag::find[]
154            FindIterable<Document> docs = crew.find();
155            // end::find[]
156            for (Document d : docs) {
157                if (!first) sb.append(",");
158                else first = false;
159                sb.append(d.toJson());
160            }
161            sb.append("]");
162        } catch (Exception e) {
163            e.printStackTrace(System.out);
164            return Response
165                .status(Response.Status.INTERNAL_SERVER_ERROR)
166                .entity("[\"Unable to list crew members!\"]")
167                .build();
168        }
169
170        return Response
171            .status(Response.Status.OK)
172            .entity(sb.toString())
173            .build();
174    }
175    // end::retrieve[]
176
177    @PUT
178    @Path("/{id}")
179    @Consumes(MediaType.APPLICATION_JSON)
180    @Produces(MediaType.APPLICATION_JSON)
181    @APIResponses({
182        @APIResponse(
183            responseCode = "200",
184            description = "Successfully updated crew member."),
185        @APIResponse(
186            responseCode = "400",
187            description = "Invalid object id or crew member configuration."),
188        @APIResponse(
189            responseCode = "404",
190            description = "Crew member object id was not found.") })
191    @Operation(summary = "Update a crew member in the database.")
192    // tag::update[]
193    public Response update(CrewMember crewMember,
194        @Parameter(
195            description = "Object id of the crew member to update.",
196            required = true
197        )
198        @PathParam("id") String id) {
199
200        JsonArray violations = getViolations(crewMember);
201        
202        if (!violations.isEmpty()) {
203            return Response
204                    .status(Response.Status.BAD_REQUEST)
205                    .entity(violations.toString())
206                    .build();
207        }
208
209        ObjectId oid;
210
211        try {
212            oid = new ObjectId(id);
213        } catch (Exception e) {
214            return Response
215                .status(Response.Status.BAD_REQUEST)
216                .entity("[\"Invalid object id!\"]")
217                .build();
218        }
219
220        // tag::getCollectionUpdate[]
221        MongoCollection<Document> crew = db.getCollection("Crew");
222        // end::getCollectionUpdate[]
223
224        // tag::queryUpdate[]
225        Document query = new Document("_id", oid);
226        // end::queryUpdate[]
227
228        // tag::crewMemberUpdate[]
229        Document newCrewMember = new Document();
230        newCrewMember.put("Name", crewMember.getName());
231        newCrewMember.put("Rank", crewMember.getRank());
232        newCrewMember.put("CrewID", crewMember.getCrewID());
233        // end::crewMemberUpdate[]
234
235        // tag::replaceOne[]
236        UpdateResult updateResult = crew.replaceOne(query, newCrewMember);
237        // end::replaceOne[]
238
239        // tag::getMatchedCount[]
240        if (updateResult.getMatchedCount() == 0) {
241        // end::getMatchedCount[]
242            return Response
243                .status(Response.Status.NOT_FOUND)
244                .entity("[\"_id was not found!\"]")
245                .build();
246        }
247
248        newCrewMember.put("_id", oid);
249
250        return Response
251            .status(Response.Status.OK)
252            .entity(newCrewMember.toJson())
253            .build();
254    }
255    // end::update[]
256
257    @DELETE
258    @Path("/{id}")
259    @Produces(MediaType.APPLICATION_JSON)
260    @APIResponses({
261        @APIResponse(
262            responseCode = "200",
263            description = "Successfully deleted crew member."),
264        @APIResponse(
265            responseCode = "400",
266            description = "Invalid object id."),
267        @APIResponse(
268            responseCode = "404",
269            description = "Crew member object id was not found.") })
270    @Operation(summary = "Delete a crew member from the database.")
271    // tag::remove[]
272    public Response remove(
273        @Parameter(
274            description = "Object id of the crew member to delete.",
275            required = true
276        )
277        @PathParam("id") String id) {
278
279        ObjectId oid;
280
281        try {
282            oid = new ObjectId(id);
283        } catch (Exception e) {
284            return Response
285                .status(Response.Status.BAD_REQUEST)
286                .entity("[\"Invalid object id!\"]")
287                .build();
288        }
289
290        // tag::getCollectionDelete[]
291        MongoCollection<Document> crew = db.getCollection("Crew");
292        // end::getCollectionDelete[]
293
294        // tag::queryDelete[]
295        Document query = new Document("_id", oid);
296        // end::queryDelete[]
297
298        // tag::deleteOne[]
299        DeleteResult deleteResult = crew.deleteOne(query);
300        // end::deleteOne[]
301        
302        // tag::getDeletedCount[]
303        if (deleteResult.getDeletedCount() == 0) {
304        // end::getDeletedCount[]
305            return Response
306                .status(Response.Status.NOT_FOUND)
307                .entity("[\"_id was not found!\"]")
308                .build();
309        }
310
311        return Response
312            .status(Response.Status.OK)
313            .entity(query.toJson())
314            .build();
315    }
316    // end::remove[]
317}

CrewMember.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2020 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License v1.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-v10.html
 8 *
 9 * Contributors:
10 *     IBM Corporation - initial API and implementation
11 *******************************************************************************/
12// end::copyright[]
13package io.openliberty.guides.application;
14
15import javax.validation.constraints.NotEmpty;
16import javax.validation.constraints.Pattern;
17
18// tag::crewMember[]
19public class CrewMember {
20    
21    @NotEmpty(message = "All crew members must have a name!")
22    private String name;
23
24    @Pattern(regexp = "(Captain|Officer|Engineer)",
25            message = "Crew member must be one of the listed ranks!")
26    private String rank;
27
28    @Pattern(regexp = "^\\d+$",
29            message = "ID Number must be a non-negative integer!")
30    private String crewID;
31
32    public String getName() {
33        return name;
34    }
35
36    public void setName(String name) {
37        this.name = name;
38    }
39
40    public String getRank() {
41        return rank;
42    }
43
44    public void setRank(String rank) {
45        this.rank = rank;
46    }
47
48    public String getCrewID(){
49        return crewID;
50    }
51
52    public void setCrewID(String crewID) {
53        this.crewID = crewID;
54    }
55
56}
57// end::crewMember[]

In this class, a Validator is used to validate a CrewMember before the database is updated. The CDI producer is used to inject a MongoDatabase into the CrewService class.

Implementing the Create operation

The add() method handles the implementation of the create operation. An instance of MongoCollection is retrieved with the MongoDatabase.getCollection() method. The Document type parameter specifies that the Document type is used to store data in the MongoCollection. Each crew member is converted into a Document, and the MongoCollection.insertOne() method inserts a new crew member document.

Implementing the Retrieve operation

The retrieve() method handles the implementation of the retrieve operation. The Crew collection is retrieved with the MongoDatabase.getCollection() method. Then, the MongoCollection.find() method retrieves a FindIterable object. This object is iterable for all the crew members documents in the collection, so each crew member document is concatenated into a String array and returned.

Implementing the Update operation

The update() method handles the implementation of the update operation. After the Crew collection is retrieved, a document is created with the specified object id and is used to query the collection. Next, a new crew member Document is created with the updated configuration. The MongoCollection.replaceOne() method is called with the query and new crew member document. This method updates all of the matching queries with the new document. Because the object id is unique in the Crew collection, only one document is updated. The MongoCollection.replaceOne() method also returns an UpdateResult instance, which determines how many documents matched the query. If there are zero matches, then the object id doesn’t exist.

Implementing the Delete operation

The remove() method handles the implementation of the delete operation. After the Crew collection is retrieved, a Document is created with the specified object id and is used to query the collection. Because the object id is unique in the Crew collection, only one document is deleted. After the document is deleted, the MongoCollection.deleteOne() method returns a DeleteResult instance, which determines how many documents were deleted. If zero documents were deleted, then the object id doesn’t exist.

Configuring the MongoDB driver and the server

MicroProfile Config makes configuring the MongoDB driver simple because all of the configuration can be set in one place and injected into the CDI producer.

Create the configuration file.
src/main/webapp/META-INF/microprofile-config.properties

microprofile-config.properties

 1# tag::hostname[]
 2mongo.hostname=localhost
 3# end::hostname[]
 4# tag::port[]
 5mongo.port=27017
 6# end::port[]
 7# tag::dbname[]
 8mongo.dbname=testdb
 9# end::dbname[]
10# tag::mongoUser[]
11mongo.user=sampleUser
12# end::mongoUser[]
13# tag::passEncoded[]
14mongo.pass.encoded={aes}APtt+/vYxxPa0jE1rhmZue9wBm3JGqFK3JR4oJdSDGWM1wLr1ckvqkqKjSB2Voty8g==
15# tag::passEncoded[]

Values such as the hostname, port, and database name for the running MongoDB instance are set in this file. The user’s username and password are also set here. For added security, the password was encoded by using the securityUtility encode command.

To create a CDI producer for MongoDB and connect over TLS, the Open Liberty server needs to be correctly configured.

Replace the server configuration file.
src/main/liberty/config/server.xml

server.xml

 1<server description="Sample Liberty server">
 2    <!-- tag::featureManager[] -->
 3    <featureManager>
 4        <!-- tag::cdiFeature[] -->
 5        <feature>cdi-2.0</feature>
 6        <!-- end::cdiFeature[] -->
 7        <!-- tag::sslFeature[] -->
 8        <feature>ssl-1.0</feature>
 9        <!-- end::sslFeature[] -->
10        <!-- tag::mpConfigFeature[] -->
11        <feature>mpConfig-2.0</feature>
12        <!-- end::mpConfigFeature[] -->
13        <!-- tag::passwordUtilFeature[] -->
14        <feature>passwordUtilities-1.0</feature>
15        <!-- end::passwordUtilFeature[] -->
16        <feature>beanValidation-2.0</feature>           
17        <feature>jaxrs-2.1</feature>
18        <feature>mpOpenAPI-2.0</feature>
19    </featureManager>
20    <!-- end::featureManager[] -->
21
22    <variable name="default.http.port" defaultValue="9080"/>
23    <variable name="default.https.port" defaultValue="9443"/>
24    <variable name="app.context.root" defaultValue="/mongo"/>
25
26    <!-- tag::httpEndpoint[] -->
27    <httpEndpoint
28        host="*" 
29        httpPort="${default.http.port}" 
30        httpsPort="${default.https.port}" 
31        id="defaultHttpEndpoint"
32    />
33    <!-- end::httpEndpoint[] -->
34
35    <!-- tag::webApplication[] -->
36    <webApplication 
37        location="guide-mongodb-intro.war" 
38        contextRoot="${app.context.root}"
39    />
40    <!-- end::webApplication[] -->
41    <!-- tag::sslContext[] -->
42    <!-- tag::keyStore[] -->
43    <keyStore
44        id="outboundTrustStore" 
45        location="${server.output.dir}/resources/security/truststore.p12"
46        password="mongodb"
47        type="PKCS12" 
48    />
49    <!-- end::keyStore[] -->
50    <!-- tag::ssl[] -->
51    <ssl 
52        id="outboundSSLContext" 
53        keyStoreRef="defaultKeyStore" 
54        trustStoreRef="outboundTrustStore" 
55        sslProtocol="TLS" 
56    />
57    <!-- end::ssl[] -->
58    <!-- end::sslContext[] -->
59</server>

The features that are required to create the CDI producer for MongoDB are Contexts and Dependency Injection (cdi-2.0), Secure Socket Layer (ssl-1.0), MicroProfile Config (mpConfig-1.4), and Password Utilities (passwordUtilities-1.0). These features are specified in the featureManager element. The Secure Socket Layer (SSL) context is configured in the server.xml file so that the application can connect to MongoDB with TLS. The keyStore element points to the truststore.p12 keystore file that was created in one of the previous sections. The ssl element specifies the defaultKeyStore as the keystore and outboundTrustStore as the truststore.

After you replace the server.xml file, the Open Liberty configuration is automatically reloaded.

Running the application

You started the Open Liberty server in dev mode at the beginning of the guide, so all the changes were automatically picked up.

Go to the http://localhost:9080/openapi/ui/ URL to see the OpenAPI user interface (UI) that provides API documentation and a client to test the API endpoints that you create after you see a message similar to the following example:

CWWKZ0001I: Application guide-mongodb-intro started in 5.715 seconds.

Try the Create operation

From the OpenAPI UI, test the create operation at the POST /api/crew endpoint by using the following code as the request body:

{
  "name": "Member1",
  "rank": "Officer",
  "crewID": "000001"
}

This request creates a new document in the Crew collection with a name of Member1, rank of Officer, and crew ID of 000001.

You’ll receive a response that contains the JSON object of the new crew member, as shown in the following example:

{
  "Name": "Member1",
  "Rank": "Officer",
  "CrewID": "000001",
  "_id": {
    "$oid": "<<ID>>"
  }
}

The <<ID>> that you receive is a unique identifier in the collection. Save this value for future commands.

Try the Retrieve operation

From the OpenAPI UI, test the read operation at the GET /api/crew endpoint. This request gets all crew member documents from the collection.

You’ll receive a response that contains an array of all the members in your crew. The response might include crew members that were created in the Try what you’ll build section of this guide:

[
  {
    "_id": {
      "$oid": "<<ID>>"
    },
    "Name": "Member1",
    "Rank": "Officer",
    "CrewID": "000001"
  }
]

Try the Update operation

From the OpenAPI UI, test the update operation at the PUT /api/crew/{id} endpoint, where the {id} parameter is the <<ID>> that you saved from the create operation. Use the following code as the request body:

{
  "name": "Member1",
  "rank": "Captain",
  "crewID": "000001"
}

This request updates the rank of the crew member that you created from Officer to Captain.

You’ll receive a response that contains the JSON object of the updated crew member, as shown in the following example:

{
  "Name": "Member1",
  "Rank": "Captain",
  "CrewID": "000001",
  "_id": {
    "$oid": "<<ID>>"
  }
}

Try the Delete operation

From the OpenAPI UI, test the delete operation at the DELETE/api/crew/{id} endpoint, where the {id} parameter is the <<ID>> that you saved from the create operation. This request removes the document that contains the specified crew member object id from the collection.

You’ll receive a response that contains the object id of the deleted crew member, as shown in the following example:

{
  "_id": {
    "$oid": "<<ID>>"
  }
}

Now, you can check out the microservice that you created by going to the http://localhost:9080/mongo/ URL.

Testing the application

Next, you’ll create integration tests to ensure that the basic operations you implemented function correctly.

Create the CrewServiceIT class.
src/test/java/it/io/openliberty/guides/application/CrewServiceIT.java

CrewServiceIT.java

  1//tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2020 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License v1.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-v10.html
  8 *
  9 * Contributors:
 10 *     IBM Corporation - initial API and implementation
 11 *******************************************************************************/
 12// end::copyright[]
 13package it.io.openliberty.guides.application;
 14
 15import static org.junit.jupiter.api.Assertions.assertEquals;
 16
 17import javax.json.*;
 18import javax.ws.rs.client.Client;
 19import javax.ws.rs.client.ClientBuilder;
 20import javax.ws.rs.client.Entity;
 21import javax.ws.rs.core.Response;
 22
 23import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
 24import org.junit.jupiter.api.*;
 25
 26import java.io.StringReader;
 27import java.util.ArrayList;
 28
 29@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 30public class CrewServiceIT {
 31
 32    private static Client client;
 33    private static JsonArray testData;
 34    private static String rootURL;
 35    private static ArrayList<String> testIDs = new ArrayList<>(2);
 36
 37    @BeforeAll
 38    public static void setup() {
 39        client = ClientBuilder.newClient();
 40        client.register(JsrJsonpProvider.class);
 41
 42        String port = System.getProperty("app.http.port");
 43        String context = System.getProperty("app.context.root");
 44        rootURL = "http://localhost:" + port + context;
 45
 46        // test data
 47        JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
 48        JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
 49        jsonBuilder.add("name", "Member1");
 50        jsonBuilder.add("crewID", "000001");
 51        jsonBuilder.add("rank", "Captain");
 52        arrayBuilder.add(jsonBuilder.build());
 53        jsonBuilder = Json.createObjectBuilder();
 54        jsonBuilder.add("name", "Member2");
 55        jsonBuilder.add("crewID", "000002");
 56        jsonBuilder.add("rank", "Engineer");
 57        arrayBuilder.add(jsonBuilder.build());
 58        testData = arrayBuilder.build();
 59    }
 60
 61    @AfterAll
 62    public static void teardown() {
 63        client.close();
 64    }
 65
 66    // tag::testAddCrewMember[]
 67    // tag::test1[]
 68    @Test
 69    // end::test1[]
 70    @Order(1)
 71    public void testAddCrewMember() {
 72        System.out.println("   === Adding " + testData.size()
 73                + " crew members to the database. ===");
 74
 75        for (int i = 0; i < testData.size(); i++) {
 76            JsonObject member = (JsonObject) testData.get(i);
 77            String url = rootURL + "/api/crew";
 78            Response response = client.target(url).request().post(Entity.json(member));
 79            this.assertResponse(url, response);
 80
 81            JsonObject newMember = response.readEntity(JsonObject.class);
 82            testIDs.add(newMember.getJsonObject("_id").getString("$oid"));
 83
 84            response.close();
 85        }
 86        System.out.println("      === Done. ===");
 87    }
 88    // end::testAddCrewMember[]
 89
 90    // tag::testUpdateCrewMember[]
 91    // tag::test2[]
 92    @Test
 93    // end::test2[]
 94    @Order(2)
 95    public void testUpdateCrewMember() {
 96        System.out.println("   === Updating crew member with id " + testIDs.get(0)
 97                + ". ===");
 98
 99        JsonObject oldMember = (JsonObject) testData.get(0);
100
101        JsonObjectBuilder newMember = Json.createObjectBuilder();
102        newMember.add("name", oldMember.get("name"));
103        newMember.add("crewID", oldMember.get("crewID"));
104        newMember.add("rank", "Officer");
105
106        String url = rootURL + "/api/crew/" + testIDs.get(0);
107        Response response = client.target(url).request()
108                .put(Entity.json(newMember.build()));
109
110        this.assertResponse(url, response);
111
112        System.out.println("      === Done. ===");
113    }
114    // end::testUpdateCrewMember[]
115
116    // tag::testGetCrewMembers[]
117    // tag::test3[]
118    @Test
119    // end::test3[]
120    @Order(3)
121    public void testGetCrewMembers() {
122        System.out.println("   === Listing crew members from the database. ===");
123
124        String url = rootURL + "/api/crew";
125        Response response = client.target(url).request().get();
126
127        this.assertResponse(url, response);
128
129        String responseText = response.readEntity(String.class);
130        JsonReader reader = Json.createReader(new StringReader(responseText));
131        JsonArray crew = reader.readArray();
132        reader.close();
133
134        int testMemberCount = 0;
135        for (JsonValue value : crew) {
136            JsonObject member = (JsonObject) value;
137            String id = member.getJsonObject("_id").getString("$oid");
138            if (testIDs.contains(id)) {
139                testMemberCount++;
140            }
141        }
142
143        assertEquals(testIDs.size(), testMemberCount,
144                "Incorrect number of testing members.");
145
146        System.out.println("      === Done. There are " + crew.size() 
147                + " crew members. ===");
148
149        response.close();
150    }
151    // end::testGetCrewMembers[]
152
153    // tag::testDeleteCrewMember[]
154    // tag::test4[]
155    @Test
156    // end::test4[]
157    @Order(4)
158    public void testDeleteCrewMember() {
159        System.out.println("   === Removing " + testIDs.size() 
160                + " crew members from the database. ===");
161
162        for (String id : testIDs) {
163            String url = rootURL + "/api/crew/" + id;
164            Response response = client.target(url).request().delete();
165            this.assertResponse(url, response);
166            response.close();
167        }
168
169        System.out.println("      === Done. ===");
170    }
171    // end::testDeleteCrewMember[]
172
173    private void assertResponse(String url, Response response) {
174        assertEquals(200, response.getStatus(), "Incorrect response code from " + url);
175    }
176}

The test methods are annotated with the @Test annotation.

The following test cases are included in this class:

  • testAddCrewMember() verifies that new members are correctly added to the database.

  • testUpdateCrewMember() verifies that a crew member’s information is correctly updated.

  • testGetCrewMembers() verifies that a list of crew members is returned by the microservice API.

  • testDeleteCrewMember() verifies that the crew members are correctly removed from the database.

Running the tests

Because you started Open Liberty in dev mode, you can run the tests by pressing the enter/return key from the command-line session where you started dev mode.

You’ll see the following output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.application.CrewServiceIT
   === Adding 2 crew members to the database. ===
      === Done. ===
   === Updating crew member with id 5df8e0a004ccc019976c7d0a. ===
      === Done. ===
   === Listing crew members from the database. ===
      === Done. There are 2 crew members. ===
   === Removing 2 crew members from the database. ===
      === Done. ===
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.411 s - in it.io.openliberty.guides.application.CrewServiceIT
Results:
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

Tearing down the environment

When you are done checking out the service, exit dev mode by pressing CTRL+C in the command-line session where you ran the server, or by typing q and then pressing the enter/return key.

Then, run the following commands to stop and remove the mongo-guide container and to remove the mongo-sample and mongo images.

docker stop mongo-guide
docker rm mongo-guide
docker rmi mongo-sample
docker rmi mongo

Great work! You’re done!

You’ve successfully accessed and persisted data to a MongoDB database from a Java microservice using Contexts and Dependency Injection (CDI) and MicroProfile Config with Open Liberty.

Learn more about MicroProfile.

Guide Attribution

Persisting data with MongoDB by Open Liberty is licensed under CC BY-ND 4.0

Copied to clipboard
Copy code block
Copy file contents

Prerequisites:

Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.

Star