2
有沒有辦法使用Resorces作爲根和子資源? 我想打電話給我的API端點這樣:帶Jersey 2子資源的RESTful URL?
GET /persons/{id}/cars # get all cars for a person
GET /cars # get all cars
如何實現我的資源使用這個網址架構?
人資源:
@Path("persons")
public class PersonsResource {
@GET
@Path("{id}/cars")
public CarsResource getPersonCars(@PathParam("id") long personId) {
return new CarsResource(personId);
}
}
汽車資源:
@Path("cars")
public class CarsResource {
private Person person;
public CarsResource(long personId) {
this.person = findPersonById(personId);
}
@GET
public List<Car> getAllCars() {
// ...
}
@GET
public List<Cars> getPersonCars() {
return this.person.getCars();
}
}