My First MicroService using Spring Boot





Welcome back to My First Tech Series (#MyFirstTechSeries). Today we will see how to create a simple micro service. Our service will perform CRUD operations on customer data such as customer name, mobile number and email address. And, we will use Postgres SQL as our backend.



STEP 1: INSTALLATION OF SPRING PLUG-IN

  • To begin, we need to have Spring Tool Suite installed in our IDE. For that, navigate to Help --> Eclipse Marketplace. Search for "Spring Tools" and hit Go. Choose to install Spring Tools 4.

  • Hit Confirm, Accept Terms and Conditions and hit Finish.



  • Please wait till the software is installed. Look out for the progress bar in the bottom right corner of eclipse IDE.

  • After installation, eclipse will ask for restart. Click Restart Now.


STEP 2: CREATE THE FIRST PROJECT


  • Navigate to File --> New --> Other --> Spring Starter Project

  • Give a name for your micro service. I have chosen demo-rest-service. Also give the group and package name. I have given com.ninja.demo. A general best practice is to give com.companyname.projectname. Also modify the description if necessary

  • Hit Next..



Add necessary dependencies. You can add them directly from here or you can later add in to the pom.xml file. For this project, I am choosing the following:

  1. Spring Web -

  2. Spring Data JDBC - for having a JDBC Connection for data access.

  3. Postgres SQL Server - this is the DB we will be using. You can choose based on your DB.

  4. Spring Data JPA - to avoid unnecessary boilerplate code. This helps in significantly reducing our coding efforts.

  5. Lombok - generates code using easy annotations. to avoid boilerplate code. Example: It automatically creates getter setter methods for entity object.




  • Click Next > and Finish.


Project is created successfully in eclipse with all our chosen dependencies available in pom.xml. This is our created project structure.


We are done with the project creation. We will move on with the next step.


STEP 3: SETTING UP THE POSTGRES SERVER

  • Download the Postgres SQL from here based on your OS and install in your system : https://www.postgresql.org/download/ At the time of writing, the version is 13.3. If you use this installation method, ignore the next bullet about brew.

  • As applicable, give the following details:

Username: postgres

Password : admin

Port : 5432 (This is default port for postgress, advisable not to change this)

  • Alternatively, You can also use brew to install in your system. I used brew. Note: It does take a few minutes to install using brew. If you use brew, then the installation command is

    brew install postgresql
    brew install --cask pgadmin4   
  • Now open pgAdmin 4 in your system. Provide a master password for pgAdmin. I have given the password as "admin". I know it's not safe, but it is easy to remember.




STEP 4: STARTING THE SPRING BOOT SERVER


Set up Application Port:

  • Often times, we get exception that "Port is already in use". To avoid this, it is always a best practice to have a dedicated port for the server. Port Number can be anything. I chose 1234. To set this port number, goto src/main/resources --> application.properties . In that file, type

server.port=1234

  • Then, we have to give Postgres SQL proper