Create a rest API to save data into database using spring boot, JPA and postman

In this tutorial let’s Create a RestAPI using spring boot and JPA and send data get requests from API using postman and insert the data into a database table, In parallel will learn how can we create an automatic database and table using JPA and spring boot.

Steps to create RestAPI into spring boot and store the data in the database.

  • Create a spring boot maven project or create by spring initializer.
  • Add required maven dependencies into POM.XML
  • Add database parameters(URL, username, and password ) into the file.
  • Create Packages, Controllers, repositories, and Entity classes.
  • Send a request from Postmen with “request body” in JSON format.
  • Get the Output.

Project Structure:

Create a Rest API in Spring boot to save data into the database

Create a maven project that will generate a pom.xml, Add some required dependencies for spring boot, JPA, MYSQL, and Lombok into pom.xml. Below is the code snippet for reference.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Online Pharmacy Management Project in Spring boot</description>




Database configuration(

Another automatic generated file is “” which will responsible for the configuration of the project, this example will do only configuration for the database.

“spring.datasource.url” is used to define the database connection URL. “createDatabaseIfNotExist=true” will create automatic database if its not exist.

“spring.datasource.driver-class-name” is used to define the database driver name.

“spring.datasource.username” and “spring.datasource.password” is used to define the username and password of the database. below is the complete database configuration code with JPA.

spring.jpa.database-platform = org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto = update

Define Entity Classes

Create a Java Class(, That contains the user attributes, getter, and setter for the user.

@Entity annotation will make the class as a entity class.

@Getter and @Setter annotation create getter and setter for all the attributes this is the beauty of Lombok.

@Table(name = “user”) is used to entity class with database table name as “user”.

@Id annotation will make the column a unique key.

@Column(name = “id” ) annotation will map it with column name “id”.

@GeneratedValue(strategy = GenerationType.IDENTITY) is used to autoincrement the unique key.

package com.MedicalStore.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;

@Table(name = "user")
public class UserEntity {
    @Column(name = "id" )
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "firstname",length = 755)
    private String firstName;
    @Column(name = "lastname",length = 755)
    private String lastName;
    @Column(name = "email",length = 755)
    private String email;
    @Column(name = "phonenumber",length = 755)
    private String phoneNumber;
    @Column(name = "login",length = 755)
    private String login;
    @Column(name = "password",length = 755)
    private String Password;

    public String toString() {
        return "UserEntity [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
                + ", phoneNumber=" + phoneNumber + ", login=" + login + ", Password=" + Password + "]";


Define Controllers in Spring boot

Create a java class(WelcomeCtl .java), below welcome controller contains two methods DisplayMessage() and

package com.MedicalStore.ctl;

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

import com.MedicalStore.entity.UserEntity;
import com.MedicalStore.repository.UserRepository;

public class WelcomeCtl {
    private UserRepository userRepository;

    public String DisaplayMessage(@RequestParam(value= "msg", required = true) String msg) {
        return msg;		
    public UserEntity save(@RequestBody UserEntity user) {		
        UserEntity save =;
        return save;		


Create Repository classes 

Repository classes extend JAPRepository which contains the method to perform database transactions. We have to create an object of Datarepositroy that will be @Autowired as a reference.

package com.MedicalStore.repository;


import com.MedicalStore.entity.UserEntity;

public interface UserRepository extends JpaRepository<UserEntity, Long>{


Application file

this will be your mail file and work as a runner when we run this class as “Java Application” or “Spring boot app”. It will start the server.

package com.MedicalStore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class MedicalStoreApplication {

    public static void main(String[] args) {, args);


Run the Spring boot application with the tomcat server. Create a request URL, with port number and request mapping.

Send a Get Request from the postman

  • Open postman and create a new request.
  • Enter the URL “http://localhost:8080/save” and body data in JSON format.
    "firstName" : "Bhupii",
    "lastName" : "mukati",
    "email" : "",
    "phoneNumber" : "8578587898",
    "login" : "mukati",
    "password" : ""
  • Send a get request from the postman
  • Check the response.
  • Refresh the database table and check data is inserted into the database.