0

我是 RESTful Web 服务的新手,需要一些帮助。我有服务将返回产品列表。URL 如下所示:

/example/product/6666,6667?expand=sellers&storeIds=2,1

要定义这个服务,我有这个接口:

@Path("/example")
public interface Service {
    @GET
    @Path("/products/{pIds}")
    @Produces( "application/json" )
    public ServiceResponse<ProductsList> getProducts(
        @PathParam("pIds") String productsIds,
        @QueryParam("expand") String expand,
        @QueryParam("storeIds") String storeIds) throws Exception;
}

我在这里假设我得到的productsIds是一个字符串,并且我需要手动将此字符串拆分为一个 id 列表,分隔符为逗号。

有没有办法将参数作为列表获取,而不是从我这边手动执行?或者是否有一个库可以用来自动执行此操作?

谢谢

4

1 回答 1

0

您可以将产品 ID 直接反序列化到一个列表中,并对您的服务定义进行一些细微的更改。试试这个:

@Path("/example")
public interface Service {
    @GET
    @Path("/products/{pIds}")
    @Produces( "application/json" )
    public ServiceResponse<ProductsList> getProducts(
        @PathParam("pIds") List<String> productsIds,
        @QueryParam("expand") String expand,
        @QueryParam("storeIds") String storeIds) throws Exception;
}

更改String productsIdsList<String> productsIds

在旁注中,我建议将产品 ID 作为查询参数传递。您的 URI 应该标识一个唯一资源(在本例中为产品)并且它应该是无状态的。

于 2013-01-28T20:23:31.273 回答