REST SERVICE using spring boot application example

This post explains a REST service implementation using springboot.

A brief about springboot:

Spring boot is a powerful framework that enables to develop applications quickly,removes hassles of maintaining configurations in XML’s,removes unnecessary boiler plate codes and abstracts wiring of different modules and its dependencies.In 2020 , this is one of the most sought after skills which every developer should know.Lets look at a simple spring boot application example and how easy it is to implement it.

Topics covered as part of this tutorial:

  • Expose a spring boot application as a REST service
  • Handle POST requests in spring boot
  • Implement business logic to handle incoming request

spring boot application example use case:

This spring boot application example takes inputs in forms of JSON as part of POST requests from clients.The application evaluates patient symptoms and data to check the likelihood of having COVID .

Parts of the application:

The application basically has 3 parts to it.

  1. REST controller : The class CovidCheckController is an implementation of REST controller. Its job is to accept inputs from the client which can be a web browser,Postman,etc . Controllers will figure out to whom it needs to route the requests to.
  2. Model : Classes in the package ‘com.ppk.covidcheck.model’ represents the patient domain. This patient domain is a set of POJO classes which represents the properties of a patient.
  3. Service : Classes in the package ‘com.ppk.covidcheck.service’ represent the service layer of this application. These classes handle the business logic.

Now lets check each part of this application.

flow of requests from clients to springboot

REST Controller:

As mentioned earlier , controllers are responsible to handle the incoming REST requests to a spring boot application. Notice the annotations @RestController , @Autowired and @PostMapping.

Model package:

This package represents the patient domain. There will be POJO classes describing patient behaviour and characteristics.

Every patient will have firstName,lastName,age and symptoms.

The symptoms is defined in another pojo Symptomps.java .

Did you notice that there are no getters and setters in the model classes?

Yes , the getters and setters are boiler plate codes which makes our code look a little ugly and clumsy . I have used Lombok to get it auto generated for me by using @Getter and @Setter annotations.

Service package:

Service package has classes which contains business logic of your application.In this class , I have written a simple logic to consume a patient object, check symptoms and return a Result object.You can use functional interfaces in java8 to handle business logic.

Testing the app:

I am using postman client to post json request to my application.Below are sample request and responses from the application.

End point URL :

http://localhost:8080/check

Sample request

Sample response

Application dependencies:

Since this is configured as a Maven project , the application dependencies are specified in pom.xml.

Code repository:

You can clone this spring boot application example code from github using this url:

https://github.com/pramodpk89/covid-check.git

You may also like...

Leave a Reply

Your email address will not be published.