EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Java
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Javachevron-right

Create a Java REST API with Quarkus and Eclipse JNoSQL for MongoDB

Otavio Santana6 min read • Published Jan 30, 2024 • Updated Jan 30, 2024
QuarkusMongoDBJava
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

In this tutorial, you will learn how to create a RESTful API using Quarkus, a framework for building Java applications, and integrate it with Eclipse JNoSQL to work with MongoDB. We will create a simple API to manage developer records.
Combining Quarkus with Eclipse JNoSQL allows you to work with NoSQL databases using a unified API, making switching between different NoSQL database systems easier.

Prerequisites

For this tutorial, you’ll need:
  • Java 17.
  • Maven.
  • A MongoDB cluster.
    • Docker (Option 1)
    • MongoDB Atlas (Option 2)
You can use the following Docker command to start a standalone MongoDB instance:
Or you can use MongoDB Atlas and try the M0 free tier to deploy your cluster.

Create a Quarkus project

  • Configure your project by selecting the desired options, such as the group and artifact ID.
  • Add the necessary dependencies to your project. For this tutorial, we will add:
    • JNoSQL Document MongoDB [quarkus-jnosql-document-mongodb]
    • RESTEasy Reactive [quarkus-resteasy-reactive]
    • RESTEasy Reactive Jackson [quarkus-resteasy-reactive-jackson]
    • OpenAPI [quarkus-smallrye-openapi]
  • Generate the project, download the ZIP file, and extract it to your preferred location. Remember that the file structure may vary with different Quarkus versions, but this should be fine for the tutorial. The core focus will be modifying the pom.xml file and source code, which remains relatively consistent across versions. Any minor structural differences should be good for your progress, and you can refer to version-specific documentation if needed for a seamless learning experience.
At this point, your pom.xml file should look like this:
By default, quarkus-jnosql-document-mongodb is in version 1.0.5, but the latest release is 3.2.2.1. You should update your pom.xml to use the latest version:

Database configuration

Before you dive into the implementation, it’s essential to configure your MongoDB database properly. In MongoDB, you must often set up credentials and specific configurations to connect to your database instance. Eclipse JNoSQL provides a flexible configuration mechanism that allows you to manage these settings efficiently.
You can find detailed configurations and setups for various databases, including MongoDB, in the Eclipse JNoSQL GitHub repository.
To run your application locally, you can configure the database name and properties in your application’s application.properties file. Open this file and add the following line to set the database name:
This configuration will enable your application to:
  • Use the “school” database.
  • Connect to the MongoDB cluster available at the provided connection string.
In production, make sure to enable access control and enforce authentication. See the security checklist for more details.
It’s worth mentioning that Eclipse JNoSQL leverages Eclipse MicroProfile Configuration, which is designed to facilitate the implementation of twelve-factor applications, especially in configuration management. It means you can override properties through environment variables, allowing you to switch between different configurations for development, testing, and production without modifying your code. This flexibility is a valuable aspect of building robust and easily deployable applications.
Now that your database is configured, you can proceed with the tutorial and create your RESTful API with Quarkus and Eclipse JNoSQL for MongoDB.

Create a developer entity

In this step, we will create a simple Developer entity using Java records. Create a new record in the src/main/java directory named Developer.

Create a REST API

Now, let’s create a RESTful API to manage developer records. Create a new class in src/main/java named DevelopersResource.

Test the REST API

Now that we've created our RESTful API for managing developer records, it's time to put it to the test. We'll demonstrate how to interact with the API using various HTTP requests and command-line tools.

Start the project:

Create a new developer with POST

You can use the POST request to create a new developer record. We'll use curl for this demonstration:
This POST request sends a JSON payload with the developer’s name and birthday to the API endpoint. You’ll receive a response with the details of the newly created developer.

Read the developers with GET

To retrieve a list of developers, you can use the GET request:
This GET request returns a list of all developers stored in the database. To fetch details of a specific developer, provide their unique id in the URL:
This request will return the developer’s information associated with the provided id.

Update a developer with PUT

You can update a developer’s information using the PUT request:
In this example, we update the developer with the given id by providing a new name and birthday in the JSON payload.

Delete a developer with DELETE

Finally, to delete a developer record, use the DELETE request:
This request removes the developer entry associated with the provided id from the database.
Following these simple steps, you can interact with your RESTful API to manage developer records effectively. These HTTP requests allow you to create, read, update, and delete developer entries, providing full control and functionality for your API.
Explore and adapt these commands to suit your specific use cases and requirements.

Using OpenAPI to test and explore your API

OpenAPI is a powerful tool that allows you to test and explore your API visually. You can access the OpenAPI documentation for your Quarkus project at the following URL:
OpenAPI provides a user-friendly interface that displays all the available endpoints and their descriptions and allows you to make API requests directly from the browser. It’s an essential tool for API development because it:
  1. Facilitates API testing: You can send requests and receive responses directly from the OpenAPI interface, making it easy to verify the functionality of your API.
  2. Generates documentation: This is crucial for developers who need to understand how to use your API effectively.
  3. Allows for exploration: You can explore all the available endpoints, their input parameters, and expected responses, which helps you understand the API’s capabilities.
  4. Assists in debugging: It shows request and response details, making identifying and resolving issues easier.
In conclusion, using OpenAPI alongside your RESTful API simplifies the testing and exploration process, improves documentation, and enhances the overall developer experience when working with your API. It’s an essential tool in modern API development practices.

Conclusion

In this tutorial, you’ve gained valuable insights into building a REST API using Quarkus and seamlessly integrating it with Eclipse JNoSQL for MongoDB. You now can efficiently manage developer records through a unified API, streamlining your NoSQL database operations. However, to take your MongoDB experience even further and leverage the full power of MongoDB Atlas, consider migrating your application to MongoDB Atlas.
MongoDB Atlas offers a powerful document model, enabling you to store data as JSON-like objects that closely resemble your application code. With MongoDB Atlas, you can harness your preferred tools and programming languages. Whether you manage your clusters through the MongoDB CLI for Atlas or embrace infrastructure-as-code (IaC) tools like Terraform or Cloudformation, MongoDB Atlas provides a seamless and scalable solution for your database needs.
Ready to explore the benefits of MongoDB Atlas? Get started now by trying MongoDB Atlas.
Access the source code used in this tutorial.
Any questions? Come chat with us in the MongoDB Community Forum.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Using MongoDB Change Streams in Java


Apr 04, 2023 | 6 min read
Article

How Queryable Encryption Can Keep James Bond Safe


Apr 02, 2024 | 2 min read
Tutorial

Java Aggregation Expression Builders in MongoDB


Apr 02, 2024 | 11 min read
Tutorial

Seamless Media Storage: Integrating Azure Blob Storage and MongoDB with Spring Boot


Dec 15, 2023 | 9 min read
Table of Contents