JavaSpringTechnology

Spring Boot Rest Service

In this article, we will write our own Spring Boot REST Service from scratch. Spring Boot’s auto-configuration comes handy when you want to concentrate on your Business rather than spending time on writing framework components and boiler plate code.

Today, we will create a Simple RESTful Service. Let’s write a Dog Service. The quickest way to get on with a Spring Boot project is to use SPRING INITIALIZR.

Tutorial Contents

1. Create Spring Boot Project

Let’s write our basic project components.

1.1 build.gradle

As of now we have added spring-boot-starter-web – denoting we want to build web application – and Lombok dependencies.

Lombok helps a lot getting rid of typical boiler plate code and I always love using it.
I generally use Lombok to avoid writing Constructors, Getters and Setters etc.
Visit Project Lombok for more information.

1.2 Application.java

Here is our starting point Application.java has the friendly public static void main method. Where we actually start a Spring Boot Application.

At this point, you have your Spring Boot application up and running. You can confirm by running the Application.java like any other Java class and see the server being up and listening for port 8080 (configurable).

2. Mock Data Provider

First let’s start and create Service and Data layer the application. As mentioned above, we are not going to have a real datastore here in the tutorial, but we will use a mock data provider which will act as a datastore for our needs.

2.1 MockDogProvider

Mock Dog Provider has an instance level pre-populated list of Dogs. The various CRUD methods in the class, actually perform the operation on the Dogs List.

2.2 DogsService

Dogs service is a routine Service Layer of our web application. It just decouples controller and data provider. Because no business logic is involved, it just delegates the calls to Data Provider.

3. Controller

We are almost there. What we need in the end is a controller which handles the REST calls and delegate to Service.

4. Run the application

4.1 Get All Dogs

Open the browser and execute (GET)
http://localhost:8080/dog

And below is the output

4.2 Get Dog by Id

Let’s make a GET request

http://localhost:8080/dogs/3

And below is the output

4.3 Add New Dog

This is a POST request I will use curl to make this call.

After this execute the GET /dogs again and you will see a new dogs has been added.

4.4 Delete a Dog

This a DELETE request using curl here.

After this you should see Dog with id 5 is gone from the list

5 Summary

So we have created our Spring Boot Rest Example. We created a Dog Service with simple Create, GET and DELETE endpoints. We have seen how easy a service is with Spring Boot where you can skip lot of crap and concentrate on your business.

In the next article we will refer to the Same Dog Service and learn Spring Rest Service Exception Handling. We will learn how spring takes care of all the crappy exceptions handling stuff lets us do more important things.

Many more articles to come, please comment for your queries and suggestions.


For full source code of the examples used here, please visit https://github.com/amitrp/dog-service


I am Technology enthusiast who loves to learn and adapt to newer programming practices and trends, and have huge passion for sharing what I know.

I work as a Technology Lead in Java Platform, and have little more than a decade of experience in Design, Code, Refactor, Test, and Deploy large scale applications as well as micro-services.
Apart from programming, I love Photography, Music creation, Fictions, Ghazals and Cooking. If you share any of the common interests, feel free to buzz. Your comments, suggestions, and appreciations are always welcome.

Leave a Reply

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