Updating Data data using HTTP POST method is commonly used to Modifying or updating existing records or entities in the database. It’s another operation of Spring CRUD. Updating data typically involves retrieving an entity, modifying its properties, and saving it back to the database.
Http POST method used to store and read data from server example and implementation explained here 
Http GET Method to retrive data from data base.

UPDATE (U):  Create RESTFul api to read data from server then modify data which you want and finally we can save it back to the table.

Step 1: Create java file BookController.java and then put below code in controller, here we have created method call updateBook(), which is expecting BookVO in Request.

package com.techbug.techbug.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.techbug.techbug.service.BookService;
import com.techbug.techbug.vo.BookVO;

public class BookController {

private BookService bookService;

@PostMapping(value = "/updatebook")
public String updateBook(@RequestBody BookVO addBookRequest) {
   return bookService.updateBook(addBookRequest);


Step 2: Create java file BookService.java and define method updateBook(), As we have seen above for update process we need to read existing data if available then update it or else don’t do anything.

package com.techbug.techbug.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.techbug.techbug.entity.Book;
import com.techbug.techbug.repository.BookRepository;
import com.techbug.techbug.vo.BookVO;

public class BookService {

private BookRepository bookRepository;

public String updateBook(BookVO addBookRequest) {
   Book book = new Book();
   // Reading existing data from table
   book = bookRepository.findBookByCode(addBookRequest.getCode());
   if(null != book) {
       // Updating data if record found
       return "Book Added Successfully !!";
   }else {
       // else returning error message
       return "NO Book Found with given book code - "+addBookRequest.getCode();


Step 3: Create java interface or JPARepository to read data from database. Here we have created custom jpa query to read data based on code, JPA provides default method as well to read based on id such as findOne(), findById() any many more.

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.techbug.techbug.entity.Book;

public interface BookRepository extends JpaRepository<Book, Long>{
   @Query("select b from Book b where b.code=?1")
   public Book findBookByCode(String code);


Step 4: Start your spring boot application now and hit the update rest end point.

First we will fetch all data from data base using read api.

before update

Trying to update book info with not available code.

no data found

Trying to update First row now

update row

Finally Pull all data to verify if update happen coorectly.

Updating Data

We can see now first row got update !! ..

Leave a Reply

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