[英]Overriding a @Path annotation in a class by method in JAX-RS
我有一個用Java維護的REST API服務(球衣,JAX-RS)
我想在服務中支持以下路線:
/api/v1/users/{userId}/cars
但是,它隱含在類的@Path
注釋中。 例如
/api/v1/cars/api/v1/users/{userId}/cars
這是我的服務班級:
@Path("api/v1/cars")
public class CarsService {
@GET
@Path("/api/v1/users/{userId}/cars")
public Response getUserCars(@PathParam("userId") Long userId) {
// ...
}
@GET
public Response getCars() {
// ...
}
}
有什么方法可以覆蓋它嗎?
請注意以下幾點:
當放置在方法上時,@ @Path
注釋不會覆蓋類的@Path
注釋。 JAX-RS / Jersey使用@Path
注釋執行分層匹配。
因此,您可以嘗試:
@Path("api/v1")
public class CarsService {
@GET
@Path("/cars")
public Response getCars() {
...
}
@GET
@Path("/users/{userId}/cars")
public Response getUserCars(@PathParam("userId") Long userId) {
...
}
}
但是,您是否考慮過使用其他資源類?
@Path("api/v1/cars")
public class CarsService {
@GET
public Response getCars() {
...
}
}
@Path("api/v1/users")
public class UsersService {
@GET
@Path("{userId}/cars")
public Response getUserCars(@PathParam("userId") Long userId) {
...
}
}
有關資源的更多詳細信息,請參閱文檔 。
您只需將方法的@Path注釋更改為:
@Path("users/{userId}/cars")
這樣,連接類和方法@Path批注的結果路徑將產生您所需的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.