Creating REST Service with Spring Boot

Creating CRUD REST Service with Spring Boot:-

This guide will help you create  REST service using Spring Boot.In a modern day, if yo, want to create a rest api in java ,  Spring Boot is the answer.Spring Boot Applications are super easy to configure and start .If you are a beginner to Spring Boot i would recomend you first understand and learn about Spring MVC.We are going to  show how to create a simple Spring Boot Application and write rest service using that.

Tools you will need:-

  • Maven 3 – Build Tool
  • Eclipse – Our Fav IDE
  • JDK 1.8
  • Spring Boot

What is REST ?:-

REST is an architectural style for designing distributed systems. It is not a standard but a set of constraints, such as being stateless, having a client/server relationship, and a uniform interface. REST is not strictly related to HTTP, but it is most commonly associated with it.

Following are the Set Of Constraints a REST Service must have:-

  1. Server:- Which delivers a service
  2. Client:- Entity which consumes the service
  3. Service is Always Stateless

What Do We Create ?:-

In order to create rest services, we are declaring some endpoints for spring Rest.A Simple rest service with following Endpoints:-

Type End Point Description
POST /Users save or add a user
GET /Users get list of all users

Every Rest endpoint needs a type associated with that. In this Rest Service with Spring Boot we are using POST and GET, there are other Http types as well such as PUT, DELETE, CONNECT, OPTIONS, HEAD.

Why Do we need these different types?

They are basically known as request methods which are treated as an indicator to our server and  perform some specific set of Actions.   Click Here and Understand More about this.

Whooh!  Let’s Create Our First Rest Service With Spring Boot !!

Create Rest Service With Spring Boot  is Super easy. Let’s Open this Url in your Browser Click  Here

It will Open Something Like in image shown below, enter the dependency as Web, package name as com.frugalis , enter any name of your choice in the name field and then click on Generate Project.We are creating a dummy application and store Users in a list and return list.

 

 

Project Structure:-

 

SampleRestApplication.Java:- 

The main method contains run() method which executes and start the spring boot.We don’t need any external tomcat as spring boot has its embedded tomcat. Once we run this Java class embedded tomcat is started.

We have used @ComponentScan(basePackages=”com.frugalis”), this annotation tells spring to scan spring beans from the specified package and load into memory.

Create Bean:-

create a bean named User.java, we are going to do all user related operations here.We are overriding equals and hashcode method to check equality.One of the books that i follow for creating and Understanding Spring boot is below at its latest edition.

User.java:-

Writing Our Service:-

We are going to create a service class and implement our logic in the service class. We are going to create a list and store user input in the User object.We are storing and retrieving from a list and return data on the basis of requested data.Let’s write a UserService interface.

UserService.java:-

UserServiceImpl.java:-

 

HelloWorldController :-

We are writing a controller which is having an annotation.@RestController.We are returning Objects from our controller methods which get automatically converted to JSON format.We are using.@PostMapping and @GetMapping to handle POST and GET requests for url /Users respectively.

 

 

POST  (/users)

GET (/users)

Rest Service SpringBoot

 

Download Code

, ,