设计子资源映射

时间:2015-06-12 18:52:24

标签: rest spring-restcontroller

住客可以预约很多。

目前我要预约住宿用户

@RequestMapping(value = "/lodgers/{lodgerId}/appointments", method = RequestMethod.GET)
public Lodger getAppointmentsByLogderId(@PathVariable("lodgerId") long lodgerId) {
    return lodgerService.getLodger(lodgerId);
}

为了获得所有日期功能用户的预约,我认为我们可以从很多方面做到这一点。

我的第一个想法是为此创建一个新的资源 在AppointmentController RestController中

@RequestMapping(value = "/appointments", method = RequestMethod.GET)
public List<Appointment> getAllAppointments(@RequestParam("startDate") Date startDate, @RequestParam("endDate") Date endDate) {
    ...
}

但我也可以这样做 在LodgerController RestController中

@RequestMapping(value = "/lodgers/appointments", method = RequestMethod.GET)
public Lodger getAllLodgerAppointmentsByDate(@RequestParam("startDate") Date startDate, @RequestParam("endDate") Date endDate) {
    ...
}

对于创作来说,这是同样的事情

Appointment is related to a lodger

@RequestMapping(value = "/lodgers/{lodgerId}appointments", method = RequestMethod.POST)
public Lodger createAppointment(@RequestParam("lodgerId")Long lodgerId, @RequestBody Appointment appointment) {
    ...
}

@RequestMapping(value = "/appointments/", method = RequestMethod.POST)
public Lodger createAppointment(@RequestBody Appointment appointment) {
    ...
}

女巫案例更好

0 个答案:

没有答案