一个请求下的多个资源:RESTful API 设计模式
Multiple resources under one request: RESTful API design pattern
我正在尝试创建一个 GET 请求,我有两个不同的请求。由于这两个资源相互关联,我试图将它们放在一个 'sub-resource' 下。
第一个是:
@QueryParam("{customerId}")
public List<customerModel> getCustomer(@QueryParam("customerId") Long customerId) {....}
这会根据 customerId 获取客户的姓名
@QueryParam("{customerId}/details")
public List<customerDetailModel> getCustomerDetail(@QueryParam("customerId") Long customerId) {....}
这会获取客户的详细信息(phone号码、地址等)
我是 运行 第一个具有以下功能的人(工作正常):
......?customerId=7453112
但是当我点击以下 URL 时我无法到达第二个请求:
......?customerId=7453112/详细信息
有什么建议吗?
您需要选择 @RequestMapping
。
类似于:
@RequestMapping("/{customerId}")
和 @RequestMapping("/{customerId}/details")
.
您的 URL 变为 7453112/details
而不是查询参数。
您可以在 @Path
注释中指定 /details
,然后使用相同的查询参数,如下所示:
@Path("/details/{customerId}")
public List<customerDetailModel> getCustomerDetail(@PathParam("customerId") Long customerId) {....}
那么您的 URL 将如下所示:
.../details/7453112
或者如果您想继续将其用作查询参数,您可以这样做:
@Path("/details")
public List<customerDetailModel> getCustomerDetail(@QueryParam("customerId") Long customerId) {....}
使用这个 url:
.../details?customerId=xxx
资源方法必须用@Path
and with a request method designator such as @GET
, @POST
, @PUT
注释,
@DELETE
, @HEAD
or @OPTIONS
. Your @QueryParam
注释放错地方了。
顺便说一下,对于这种情况,您应该考虑使用 path 参数而不是 query 参数。因此你应该使用 @PathParam
instead of @QueryParam
in the method parameter. For more details on when to use query or path parameters, have a look at this answer
此外,如果您通过其唯一标识符请求资源,则不确定为什么要 returning List
。在这种情况下,您应该 return 单个资源的表示(而不是多个资源的表示)。
因此您的资源 class 将如下所示:
@Path("customers")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class CustomerResource {
@GET
@Path("{customerId}")
public CustomerModel getCustomer(@PathParam("customerId") Long id) {
...
}
@GET
@Path("{customerId}/details")
public CustomerDetailModel getCustomerDetail(@PathParam("customerId") Long id) {
...
}
}
第一个端点可以请求如下:
GET http://example.com/api/customers/1
第二个端点可以请求如下:
GET http://example.com/api/customers/1/details
我正在尝试创建一个 GET 请求,我有两个不同的请求。由于这两个资源相互关联,我试图将它们放在一个 'sub-resource' 下。 第一个是:
@QueryParam("{customerId}")
public List<customerModel> getCustomer(@QueryParam("customerId") Long customerId) {....}
这会根据 customerId 获取客户的姓名
@QueryParam("{customerId}/details")
public List<customerDetailModel> getCustomerDetail(@QueryParam("customerId") Long customerId) {....}
这会获取客户的详细信息(phone号码、地址等)
我是 运行 第一个具有以下功能的人(工作正常): ......?customerId=7453112
但是当我点击以下 URL 时我无法到达第二个请求: ......?customerId=7453112/详细信息
有什么建议吗?
您需要选择 @RequestMapping
。
类似于:
@RequestMapping("/{customerId}")
和 @RequestMapping("/{customerId}/details")
.
您的 URL 变为 7453112/details
而不是查询参数。
您可以在 @Path
注释中指定 /details
,然后使用相同的查询参数,如下所示:
@Path("/details/{customerId}")
public List<customerDetailModel> getCustomerDetail(@PathParam("customerId") Long customerId) {....}
那么您的 URL 将如下所示:
.../details/7453112
或者如果您想继续将其用作查询参数,您可以这样做:
@Path("/details")
public List<customerDetailModel> getCustomerDetail(@QueryParam("customerId") Long customerId) {....}
使用这个 url:
.../details?customerId=xxx
资源方法必须用@Path
and with a request method designator such as @GET
, @POST
, @PUT
注释,
@DELETE
, @HEAD
or @OPTIONS
. Your @QueryParam
注释放错地方了。
顺便说一下,对于这种情况,您应该考虑使用 path 参数而不是 query 参数。因此你应该使用 @PathParam
instead of @QueryParam
in the method parameter. For more details on when to use query or path parameters, have a look at this answer
此外,如果您通过其唯一标识符请求资源,则不确定为什么要 returning List
。在这种情况下,您应该 return 单个资源的表示(而不是多个资源的表示)。
因此您的资源 class 将如下所示:
@Path("customers")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class CustomerResource {
@GET
@Path("{customerId}")
public CustomerModel getCustomer(@PathParam("customerId") Long id) {
...
}
@GET
@Path("{customerId}/details")
public CustomerDetailModel getCustomerDetail(@PathParam("customerId") Long id) {
...
}
}
第一个端点可以请求如下:
GET http://example.com/api/customers/1
第二个端点可以请求如下:
GET http://example.com/api/customers/1/details