该CountryResource
类需要有一个方法注释@Path
到子资源CityResource
。默认情况下,您有责任创建例如CityResource
的实例
@Path("country/state/{stateName}")
class CountryResouce {
@PathParam("stateName")
private String stateName;
@Path("city/{cityName}")
public CityResource city(@PathParam("cityName") String cityName) {
State state = getStateByName(stateName);
City city = state.getCityByName(cityName);
return new CityResource(city);
}
}
class CityResource {
private City city;
public CityResource(City city) {
this.city = city;
}
@GET
public Response get() {
// Replace with whatever you would normally do to represent this resource
// using the City object as needed
return Response.ok().build();
}
}
CityResource
提供了处理HTTP动词(GET
在这种情况下)的方法。
有关更多信息的子资源定位器,您应该查看Jersey documentation。
另请注意,Jersey提供ResourceContext以获得它来实例化子资源。如果您打算在子资源中使用@PathParam
或@QueryParam
,我相信您需要使用它,因为在通过new
自行创建时,运行时不会触及子资源。
现在你告诉我!我一直在手动创建文件,然后将它们导入到Eclipse中。 –