Implementation for update method in spring boot

Viewed 56

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));
}
1 Answers

Following the RFC 7231, the answer is yes "The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload." You need to pass the id on the URL, because your book is a resource, so there must be a way to find it, since an URL is a resource locator.

Related