Skip to main content

How to Connect to Spring BOOT Rest service to Mongo DB in Docker

 Connect Spring BOOT Rest Service To MONGO DB within Docker

SpringBoot is a  popular spring framework that allows users to create projects capable of running as standalone executables . It fits in so well in microservices architecture where each service is supposed to be running independently. Spring boot can easily be integrated with different backend database servers like mongo, ms sql, oracle db etc.

In this Tutorial,we will

  • Configure Spring Boot project for Mongo
  • Add  code for storing/retrieving data
  • Create docker image of our project
  • Link to mongo docker image
  • Test the application

Configure Spring Boot project for Mongo

    • Assuming that you already have a Spring BOOT Rest services project in your workspace, if not create one by using below link.
  • Add Mongo DB dependencies in your pom.xml

  • In your application.properties add the following line to connect to MongoDB inside

Add  code for storing/retrieving data

  • Create a Java class Employee with some fields

 

  • In your code create an interface which extends MongoRepository

  • Create a controller class which has a post to create an employee record  and get method to view an employee record

  • Create a main class to run this application.

  • You can get/clone the complete source code from Github
  • Build your project using the following command.This should create a jar file inside target directory under project root directory

Create docker image of our project

  • Once your jar is generated, create a Docker File with following contents.Name this file as Dockerfile and place it under root directory of your project

  • Dspring.data.mongodb.uri=mongodb://mongo/test, in this line we are telling that point to the container named mongo and test DB.
  • Now let us build a docker image using following command line, make sure that you navigate to the same directory using terminal or command prompt in which you have created the Dockerfile

  • See if the image has been created using the following command

  • You should see something like this in your console

Link to mongo docker image

  • Now in our Docker container, we need to make sure that we have a Docker image of mongo database running and the name of that image should be mongo(this is important docker ps –a will show you the name of the far right column). If you don’t have mongo image locally the following image will get the official mongo image from docker hub

 

  • What we are saying here is that running mongo dB container on port 27017 should be mapped to localhost port 27000. The –name suggests that we are naming the container to mongo. The Docker assigns some random names every time on restart so make use of –-name.Now we need to run our Spring Boot Image and link it to mongo DB using following command.

Test the application

  • Open rest client like Advanced Rest client/Postman and post the following request to this url http://localhost:8080/employee

  • If you get response 200 OK, as below that would mean a record has been created.

  • To view the inserted request, do a Get Request with the following URL. http://localhost:8080/ID_RETURNED_IN PREVIOUS CALL
  • You should be able to see the below record.

 

Get the above code from GitHub

Related Posts

Leave a Reply

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

Bitnami