在你的情況我會把邏輯加載的人約會在一個資源並使其從其他資源訪問(換句話說委派資源,有什麼我假設你已經這樣做)。實際的邏輯存儲在AppointmentService中,因此您可以將它與getPersonAppointments()一起使用。
這樣你就可以把它叫做:
- /人/ 1 /約會
- /約會PERSONID = 1
兩個導致進入同樣的反應? 這可能不是最佳實踐,但邏輯集中在一個地方,使其更具可讀性和可重用性。
@Path("person")
public class PersonResource {
@GET
@Path("{personId: [0-9]+}/appointments")
public AppointmentsResource loadAppointmentsForPerson(@PathParam("personId") Long personId) {
return new AppointmentsResource(personId);
}
}
@Path("apointments")
class AppointmentResource {
private AppointmentService service;
private Long personId;
public AppointmentResource() {
}
public AppointmentResource(Long personId) {
this.personId = personId;
}
@GET
public Response loadAppointmentsForPerson(@QueryParam("personId") Long personId) {
Long personIdToUse = null;
if (this.personId != null) {
personIdToUse = this.personId;
} else if (personId != null) {
personIdToUse = personId;
} else {
//no input, bad request
}
List<Appointment> appointments = service.getPersonAppointments(personIdToUse)
//create your response from appointments
}
}
當然,在你的AppointmentResource中,你會有另一個東西,比如通過特定ID加載約會。