REST APIs with Java, Spring Boot, and MongoDB
Rate this code example
So if you want to start writing REST APIs in Java, clone this project, and you will be up to speed in no time.
That’s all folks! All you need is in this repository. Below I will explain a few of the features and details about this template, but feel free to skip what is not necessary for your understanding.
The important section here is the MongoDB configuration, of course. Firstly, you will notice the connection string is automatically retrieved from the
application.properties file, and secondly, you will notice the configuration of the
Codec is the interface that abstracts the processes of decoding a BSON value into a Java object and encoding a Java object into a BSON value.
CodecRegistry contains a set of
Codec instances that are accessed according to the Java classes that they encode from and decode to.
The MongoDB driver is capable of encoding and decoding BSON for us, so we do not have to take care of this anymore. All the configuration we need for this project to run is here and nowhere else.
Here is an example:
As you can see, I’m using an auto-closeable try-with-resources which will automatically close the client session at the end. This helps me to keep the code clean and simple.
Some of you may argue that it is actually too simple because transactions (and write operations, in general) can throw exceptions, and I’m not handling any of them here… You are absolutely right and this is an excellent transition to the next part of this article.
If retryable writes are disabled or if a write operation fails twice, then MongoDB will send a MongoException (extends RuntimeException) which should be handled properly.
Luckily, Spring provides the annotation
ExceptionHandler to help us do that. See the code in my controller
PersonController. Of course, you will need to adapt and enhance this in your real project, but you have the main idea here.
Just to give you a small example, I implemented the
/api/persons/averageAge route to show you how I can retrieve the average age of the persons in my collection.
Also, you can note here that I’m using the
personCollection which was initially instantiated like this:
Normally, my personCollection should encode and decode
PersonEntity object only, but you can overwrite the type of object your collection is manipulating to return something different — in my case,
AverageAgeDTO.class as I’m not expecting a
PersonEntity class here but a POJO that contains only the average age of my "persons".
You can test the REST APIs from this web page and explore the models. Don’t forget to disable it in production. ;-)
That's all I did to make it work:
Yes, there is a Nyan Cat section in this post. Nyan Cat is love, and you need some Nyan Cat in your projects. :-)
Did you know that you can replace the Spring Boot logo in the logs with pretty much anything you want?
Well, now you know. You are welcome.
Have a look at the
banner.txt file if you want to replace this awesome Nyan Cat with your own custom logo.
I hope you like my template, and I hope I will help you be more productive with MongoDB and the Java stack.