該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中。 –