春季启动中更新方法的实现
我想编写一种更新书籍的方法。我通过的参数是更新的书籍和同一本书的ID。我对实现更新方法的确切方法感到困惑。我是否应该在路径参数中传递ID,因为ID已经包含在我通过的更新书中。
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));
}
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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在RFC 7231之后,答案是肯定的:
创建目标资源的状态或用由请求消息有效载荷中包含的表示形式定义的状态来创建或替换。“您需要在URL上传递ID,因为您的书是资源,由于URL是资源定位器。
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.
由于这本书是资源,因此您应该将ID传递给请求,尽管它已经通过请求主体中的书籍资源进入了。但是您需要在路径参数中指定ID
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