【发布时间】:2017-04-20 12:12:29
【问题描述】:
有没有办法将 Resorces 用作根资源和子资源? 我想这样调用我的 api 端点:
GET /persons/{id}/cars # get all cars for a person
GET /cars # get all cars
如何实现我的资源以使用此 url 架构?
人物资源:
@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();
}
}
【问题讨论】:
标签: java rest jersey-2.0