Skip to content

OpenLiberty/sample-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Sample

This sample shows how to store data with MongoDB using CDI and MicroProfile Config, as well as data validation with Jakarta Bean Validation.

Environment Set Up

To run this sample, first download or clone this repo - to clone:

git clone git@github.com:OpenLiberty/sample-mongodb.git

Setup MongoDB

You will also need a MongoDB instance to use this sample. If you have Docker installed, you can use the following:

docker build -t liberty_mongo mongo
docker run --name liberty_mongo -d -p 27017:27017 liberty_mongo

If you don't have Docker, you can install MongoDB manually from mongodb.com

Next, you will need to create a user for authentication. Issue the following commands from the command line:

If you're using docker, you can skip this step. It is done for you by the init.js file.

mongosh
use testdb
db.createUser({user: 'sampleUser', pwd:'openliberty', roles: [{ role: 'readWrite', db:'testdb'}]})

You should see the following:

{ ok: 1 }

Now you are ready to run the sample. Type exit to get out of the mongo shell, and if using docker type exit again to exit the docker shell.

Running the Sample

From inside the sample-mongodb directory, build and start the application in Open Liberty with the following command:

./mvnw liberty:dev

Once the server has started, the application is availible at http://localhost:9080

Try it out

Give the sample a try by registering a crew member. Enter a name (a String), an ID Number (an Integer), and select a Rank from the menu, then click 'Register Crew Member'.

Two more boxes will appear, one with your crew members (which you can click to remove) and one showing how your data looks in MongoDB.

Stop MongoDB

If you started MongoDB using docker, you can stop the container with:

docker stop liberty_mongo

How it works

This application uses a CDI producer (MongoProducer.java) to inject a MongoDatabase. For more info on using a CDI producer with MongoDB, check out this blog post. It provides access to the database in a RESTful manner in CrewService.java using the /mongo/db endpoint.

Calling POST /{id} on the endpoint uses Bean Validation to validate the data we recieve from the front end. CrewMember.java shows the constraints as well as the messages we return to the user if those constraints aren't met.

@NotEmpty(message = "All crew members must have a name!")
private String name;

@Pattern(regexp = "(Captain|Officer|Engineer)",  message = "Crew member must be one of the listed ranks!")
private String rank;

@Pattern(regexp = "^\\d+$", message = "ID Number must be a non-negative integer!")
private String crewID; 

After validation, we use the injected MongoDatabase to insert a new document with the crew member's information:

MongoCollection<Document> crew = db.getCollection("Crew");
Document newCrewMember = new Document();
newCrewMember.put("Name",crewMember.getName());
newCrewMember.put("Rank",crewMember.getRank());
newCrewMember.put("CrewID",crewMember.getCrewID());
crew.insertOne(newCrewMember);

Calling DELETE /{id} on the endpoint deletes a document corrosponding to the path parameter {id}

crew.deleteOne(new Document("_id", new ObjectId(id))); 

Calling GET on the endpoint retrives the data and does some formatting for the front end.

MongoCollection<Document> crew = db.getCollection("Crew");
for (Document d : crew.find()) {
	sb.append(d.toJson());
}