是否可以将路径存储作为列表存储?

时间:2015-10-28 17:52:24

标签: java rest jax-rs

我有一个Rest服务,我想用以下路径回复请求

1)/v1/config/type/service

2)/v1/config/type/service, service2

我想要的是能够将路径参数serviceName存储为List,其中每个元素都以逗号分隔。例如,如果某人键入v1/config/foo/bar1,bar2,bar3,我希望serviceNameList,其中包含3个元素(bar1,bar2,bar3)。现在它只返回一个包含1个元素的列表,其中包含所有三个服务字符串。这甚至可能吗?或者是我只需要解析的东西。我的代码如下所示,由于我处于项目的开始阶段,因此非常粗糙:

@ApplicationPath("/")
@Path("/v1/config")
public class ServiceRetriever extends Application { 

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHelloWorld() {
    return "Hello World";
}

@GET
@Path("{type}/{serviceName}")
@Produces("application/zip")
public Response getServices(@PathParam("type") String type, @PathParam("serviceName")List<String> serviceNames, 
        @QueryParam("with_config") boolean withConfig, @QueryParam("with_drive") boolean withDriver) throws IOException
{
    //some random file i made to test that we can return a zip
    File file = new File(System.getProperty("user.home")+"/dummy.zip");
    System.out.println(serviceNames.size()); //returns 1

    //we can change the zip file name to be whatever 
    return Response.ok(file).header("Content-Type","application/zip").
            header("Content-Disposition", "attachment; filename="+file.getName()).build();
}

1 个答案:

答案 0 :(得分:0)

问题是你必须改变该变量的反序列化过程。通常,只有查询参数是列表,因此这可能与某些库不兼容。 你可以:

  • 将参数捕获为字符串,并通过帮助方法(显而易见)
  • 在内部解析它
  • 创建自己的注释,如@PathParamMutli并返回Arrays.asList(parameter.split(","));。理想情况下,您应该可以访问框架源代码和分支权限。

  • 使用查询参数

相关问题