我第一次在 Spring 中设计 API,我有以下用例:
- 获取所有产品。
- 按 ID 获取产品。
现在,我所做的是,我为此制作了一个 API,
@Path("/products")
public interface ProductResource {
@GET
@ApiOperation(value = "Gets all the products by criteria")
Response getProductsByCriteria(@Context UriInfo uriInfo);
}
我正在做的是,我在查询参数中获取 ID。如果它的值为空,我将调用该方法(在服务层)返回所有产品,否则我将调用该方法返回基于其 id 的特定产品(进入查询参数)。
我只想知道,这是一个糟糕的 RESTFul 设计吗?我是否应该使用两个单独的 API 来根据他们的 ID 获取产品并获取所有产品?