春季启动中更新方法的实现

发布于 2025-02-11 20:28:13 字数 720 浏览 2 评论 0原文

我想编写一种更新书籍的方法。我通过的参数是更新的书籍和同一本书的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2025-02-18 20:28:13

在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.

甜尕妞 2025-02-18 20:28:13

由于这本书是资源,因此您应该将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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文