Search code examples
javaspringspring-bootjersey

Implementation for update method in spring boot


I want to write a method for updating a book. The parameters I am passing are updated book and the id of the same book. I am confused about the exact way to implement the update method. Should I pass id in the path param or not because id is already contained in the updated book which I am passing.

BookController

 @PUT
@Path("/{isbn}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public BookResourceRto updateBook(BookResourceRto book, @PathParam("isbn") String isbn){

    Book updatedBook = bookRtoTransformer.apply(updatedBookRto);

    return bookModelTransformer.apply(bookService.updateBook(updatedBook));}

BookService

public Book updateBook(Book updatedBook) {

    BookRepoRto bookRepoRto = bookModelTransformer.apply(updatedBook);

    return bookRepoRtoTransformer.apply(bookRepository.save(bookRepoRto));
}

Solution

  • As the book is a resource, you should pass the id to the request, though it is already getting in through the book resource in request body. But you need to specify the id in the path param