Upload file to MongoDB using Jersey 2

The NoSQL databases are gaining increasing popularity. MongoDB, being one of the most established among them, uses JSON data model and offers great scalability and ease of use due to the dynamic data schemas.

If you are building a MongoDB based backend for your application, be it web or mobile, it is easy to use RESTful web services to build an interface layer between the network (http) and your database. The JAX-RS specification (JSR 339) is a part of the Java EE platform and provides a standardised and neat way how to create REST services in Java. Jersey 2, the reference implementation of the mentioned JSR, comes with lots of extensions and has the benefit of being able to run in a non-servlet environment, on a lightweight Grizzly container.

Install and run MongoDB

This is an obvious and easy step. You can follow the instructions on the MongoDB website.

When you’ve got your DB daemon up and running, you can connect to it with the mongo console and try some basic commands. To learn the basic stuff, there is a web-based training console to guide you through the first steps.

Create a simple Jersey resource

Our resource methods has two parameters, the first one is the actual input stream of the uploaded file and the second one is actually not necessary, but usually you want to attach the data to some sort of entity or category. In this case, the parameter is the string object id of an entry in the Mongo database (it is a good practice to use the object id construct rather than forcing own numeric identifiers to entries, as it would affect the scalability of the database).

Database access

The database access object (DAO) referenced from the endpoint listed above will handle all the needed communication with the underlying DB engine.

But first we need to obtain the connection. In this example, we will go just with a simple singleton object encapsulating the initial connection and exposing the com.mongodb.DB  instance.

Now, we can access the DB instance from our DAO.

And finally, let’s persist the stream content. Although it is possible to save some BSON data directly in the Mongo collection (the BinData  type), but there are some limitations (i.e. file size) and other drawbacks. MongoDB provides a specification to come across that called GridFS, which stores the binary data in smaller chunks.

Build the project and deploy on Grizzly

Main class

One of the Jersey benefits is the broad variety of environment it is capable to run in. Thus, we don’t have to create deployment descriptors as for lot of use cases, we don’t actually need a servlet container.  Jersey supports more low-level containers, one of our favourites is the already mentioned Grizzly.

We need to create a simple launcher, a small class that programatically starts Grizzly and exposes the Jersey resources.

Please, note that the use of packages()  method, so called package scanning, is a nice shortcut for development but it is not necessarily the best choice for production – it is preferable to explicitly add class by class and register feature by feature into the resource config (also referred to as Application subclass).


Even a minimalistic project, if using frameworks and libraries, becomes challenging in terms of dependency management and having class path set up correctly.

Using maven will save us most of this hassle. Here’s what the project object model, our pom.xml  file looks like:

If you followed the standard maven project structure, you can now easily build the example using e.g.


and subsequently run the application with:




From the client side, you just need to invoke the multipart/form-data  http POST request, e.g. from the webpage using the file upload form element, JavaScript or any other method you might prefer.

Check the result

To see if everything worked, start the database console:

and check if new entry is present in the collection:

And you should see something like this:

Of course, the example is not really complete, contains close to no error handling, the uploaded file does not have the content type set and first of all, the sample uses blocking I/O and as such is not very scalable, but the intent was to show the basic file upload code using Jersey and MongoDB.

Now just go ahead and tweak the code to suit your needs, have fun with that and create something useful.


Leave a Reply

Your email address will not be published. Required fields are marked *