编写休息终点的好练习是什么?
我想添加一个休息端,以增加酒店中某些房间的预订。更好的方法是什么?我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
评论中的建议肯定会有所帮助。我建议拨打呼叫服务类以进行实施。您可以在控制器类中自动自动化服务类,然后实现功能。此外,我可以建议您可以使用响应对象发送响应。
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.
我认为这毕竟取决于数据库的结构,尽管遵循纯粹的休息规则,但我会选择选项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