我们应该在REST API的path-params,query-params或JSON请求中使用枚举常量吗?

时间:2017-07-18 19:04:50

标签: java json rest jax-rs

问题是关于REST API的最佳实践。

示例:我有一个带有path-param的get API作为状态。

/api/{status}

现在,在代码中,状态被定义为枚举(状态)。只有3个(或一些小的有限)可能的状态值。

public enum Status {
    created,
    completed,
    cancelled
}

所以,我应该接受" status"作为字符串或枚举类型?

@Path("/api/{status}")
@GET
public Response get(@PathParam("status") String status) {}
// then I can check if it is a valid status

OR

@Path("/api/{status}")
@GET
public Response get(@PathParam("status") Status status) {}
// it will throw an exception if it is an invalid status

另外,我想了解有关query-params和JSON请求的最佳实践。

0 个答案:

没有答案