Project Step by Step for REST API

Step 1) Create new spring starter project.

create Spring Starter Project via Spring Initializr Web Service

Step 2) Mention below configurations. Click Next.

create Spring Starter Project via Spring Initializr Web Service

Click next and select checkboxes below. This will bring jars automatically to our project.

create Spring Starter Project via Spring Initializr Web Service

create Spring Starter Project via Spring Initializr Web Service

Click Finish.

Below will be project structure you will see in explorer.

Spring Boot REST API project structure

Now try running JbkSpringBootRestApiApplication.java (you can change this to any name suitable for our project).

If you see below logo on console means our application started correctly.

spring console logo

Now go ahead and create REST API.

Write pojo which holds data in format of java object.

Transaction.java as a standard add constructor getter and setters.


Now I will create endpoint in controller. This is a class where user requests will be coming in. This class will be able to produce out put in json or xml format.

The entity which sends request has to consume this response and may be rendered on UI.

TrasactionController.java


If you observe we have Transaction and Transaction controller in other package so we need to tell spring boot class where my controllers and pojo are.

One more thing whenever we write Transaction.java to be autowired annotate this class with @Service or @Repository or @Component all are same but for used different purposes as per design.

I will use @component as this is just pojo. But if some class is doing persistence logic or database operation I may go for @Repository.

If lot of business logic is handled I will go with @Service. These are common conventions people are using.

Main class will look like this.


Run application you will see below on browser.

One trouble shooting I will do here if below is error.

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

Then stop server first by clicking below red button in console.

spring console

After this you will see terminate button click that.

spring console

Click that and run main class again.

spring console logo

This appeared means we are good to go.

Go to browser put URL as http://localhost:8080/transaction/get in address bar.

run spring boot rest api application on localhost

Now let’s put logic in controller to see proper values in response.

Modify method as below:


In response on browser you will see:


Now we will see some more things


URL: http://localhost:8080/transaction/getId

Response will be see if nothing provided in request default value will be taken.


URL: http://localhost:8080/transaction/getId?transId=890

Response will be as below:


In controller add method as below:


URL: http://localhost:8080/transaction/getAllTrans