编写休息终点的好练习是什么?

发布于 2025-02-12 22:23:34 字数 1008 浏览 1 评论 0原文

我想添加一个休息端,以增加酒店中某些房间的预订。更好的方法是什么?我有2个解决方案:

@RequestMapping("api/v1/hotels/hotelId/rooms/roomId/reservations")
some Class Endpoint

    @PostMapping
    public ReservationApi save(final @RequestBody ReservationApi reservationApi,
                               final @PathVariable("hotelId") String hotelId,
                               final @PathVariable("roomId") String roomId) {
        final Reservation reservation = reservationMapper.toDomain(reservationApi);
        return reservationMapper.toApi(reservationService.save(reservation, hotelId, roomId));
    }

并且在服务方面构建保留

或者我有一个不使用路径的替代解决方案,仅使用API​​类来创建:

@RequestMapping("api/v1/reservations")

    @PostMapping
    public ReservationApi save(final @RequestBody ReservationApi reservationApi) {
        final Reservation reservation = reservationMapper.toDomain(reservationApi);
        return reservationMapper.toApi(reservationService.save(reservation));
    }

I want to add a REST endpoint that will add reservations for some rooms in a hotel. What is the better way to do it? I have 2 solutions:

@RequestMapping("api/v1/hotels/hotelId/rooms/roomId/reservations")
some Class Endpoint

    @PostMapping
    public ReservationApi save(final @RequestBody ReservationApi reservationApi,
                               final @PathVariable("hotelId") String hotelId,
                               final @PathVariable("roomId") String roomId) {
        final Reservation reservation = reservationMapper.toDomain(reservationApi);
        return reservationMapper.toApi(reservationService.save(reservation, hotelId, roomId));
    }

And build Reservation on the service side:

Or I have an alternative solution where we do not use paths, only an API class is used for creating:

@RequestMapping("api/v1/reservations")

    @PostMapping
    public ReservationApi save(final @RequestBody ReservationApi reservationApi) {
        final Reservation reservation = reservationMapper.toDomain(reservationApi);
        return reservationMapper.toApi(reservationService.save(reservation));
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青萝楚歌 2025-02-19 22:23:34

评论中的建议肯定会有所帮助。我建议拨打呼叫服务类以进行实施。您可以在控制器类中自动自动化服务类,然后实现功能。此外,我可以建议您可以使用响应对象发送响应。

The suggestions in the comments are for sure helpful. I would suggest to go with calling the calling the service class for the implementation. You can autowire the service class inside the controller class and then implement the functionality. Additionally, I can suggest that you can use ResponseEntity object for sending the response.

牵你的手,一向走下去 2025-02-19 22:23:34

我认为这毕竟取决于数据库的结构,尽管遵循纯粹的休息规则,但我会选择选项1,为什么?因为如果我想以后查看房间的预订清单,我会使用与用来为该房间创建预订的相同端点。

此外,我们不知道Reservationapi中有什么 - 我们还提供了那里的酒店ID和房间ID,还是仅提供预订所需的信息?

我值得创建保留响应,我会选择选项1

我同意以前的评论,即如果我必须创建这样的API,

I think it depends on the structure of the database after all, although following pure REST rules, I would choose option 1, why? because if I want to review the booking list for a room later, I use the same endpoint as I used to create a booking for that room.

In addition, we do not know what is in the ReservationApi - do we also provide the hotel id and room id there, or only the information that is needed to create a reservation?

I agree with the previous comments that it's worth creating a ReservationResponse

if I had to create such an api, I would choose option 1

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