Cyber Guard
Fight against cyber crime

Spring boot web service using Spring data JPA

What is Web Service

Web Service is a service provided by a web application to another application through internet or intranet using HTTP as protocol.

The service which the provider provides that mostly data in a globally accepted predefined manner. The data which the provider provides that is mostly in JSON or XML format. The consumer application connects to the provider application through HTTP and consumes data available in JSON or XML format. Before going to Spring boot web service using Spring JPA, we should have a look at the following picture.

Spring boot web service using Spring JPA
Producer and Consumer in any type of web service

Things to know before developing Spring boot web service using Spring data JPA

Use the tutorial here to know Sample

Go through the following video tutorial regarding Spring boot web service.

Spring Boot web service using spring boot (This does not use data from DBMS)

What is Spring data JPA

A set of API available in Spring framework to communicate with any DBMS.

Spring Data JPA is not an implementation or JPA provider, it’s just an abstraction used to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. 

Spring Data JPA always requires the JPA provider such as Hibernate or Eclipse Link.

Example video regarding Spring data JPA in Spring Boot.

Requirement to Build Spring boot web service using Spring data JPA

  • A Spring Boot starter web application in STS or Eclipse

Use this URL to know how to create Spring Boot Starter application

  • A DataBase in any DBMS. This tutorial use MySQL from XAMPP.
  • Addition of maven dependencies for Spring data JPA, Java Persistency API and DBMS connector in pom.xml
  • Addition of DataBase properties in application.properties file present in spring boot application.
  • A class to represent table . Such type of class called as Entity
  • An interface to represent Spring data JPA API. Such type of interface called as Repository.
  • A class to represent web service end point. Such type of class called as rest controller.

After creating DataBase in MySQL, the phpMyAdmin interface of XAMPP will like follows.

A DataBase named “covid19” created in MySQL available from XAMPP.

Add Dependencies in pom.xml

<dependencies>
.....................................
............Pre-existing Dependencies.....
......................................
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

Add DataBase properties in applications.properties

spring.datasource.url= jdbc:mysql://localhost/covid19
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update

Create Entity class in Spring Boot project. The class should be created in a package. Lets name the file as Patient.java

Patient.java

package com.cyber.deb.covid19.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Patient {
@Id
@GeneratedValue
int slno;
String name;
int age;
double temperature;
public int getSlno() {
return slno;
}
public void setSlno(int slno) {
this.slno = slno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}


}

Create Repository interface in Spring Boot project. The interface should be created in a package and the interface must inherit JpaRepository. Lets name the file as PatientRepository.java

PatientRepository.java

package com.cyber.deb.covid19.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.cyber.deb.covid19.entities.Patient;
public interface PatientRepository extends JpaRepository {
}

Create REST Controller class in Spring Boot project. The class should be created in a package. Lets name the file as PatientController.java. The class must be annotated using @RestController

PatientController.java

package com.cyber.deb.covid19.controllers;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.cyber.deb.covid19.entities.Patient;
import com.cyber.deb.covid19.repositories.PatientRepository;


@RestController
public class PatientController {
@Autowired
PatientRepository patientRepository;
@RequestMapping("/showAll")
public List<Patient> showAllPatient()
{
List<Patient> patients=patientRepository.findAll();
return patients;
}
}

Project explorer after code completion

Project explorer after code completion
Project explorer view after code completion

Run the Spring boot Starter Application

Right click on project—– Run As — Spring Boot App (In STS)

Right click on Java file of main()—- Run As–Java Application (In Eclipse)

Following tables get created by Spring boot App

Screen Shot of DataBase named Covid19 in web browser
Screen Shot of DataBase named Covid19 in web browser

Add records into “patient” table

Screen Shot of inserting into Patient table.
Screen Shot of inserting into Patient table.

Data in “patient” table

Screen Shot of patient table in "covid19" DataBase
Screen Shot of patient table in “covid19” DataBase

Output in browser by using endpoint URL of Web Service localhost:8080/showAll

RESTful web service output in web browser
RESTful web service output in web browser

Output in postman by using endpoint URL of Web Service localhost:8080/showAll

Output in Postman of Spring Boot RESTful web Service using Spring data JPA
Output in Postman of Spring Boot RESTful web Service using Spring data JPA

Download the full source code

Zip download of Spring boot web service using Spring data JPA

Share

You may also like...

Leave a Reply

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