4

我的功能如下所示:

    @PUT
    @Path("property/{uuid}/{key}/{value}")
    @Produces("application/xml")    
    public Map<String,ValueEntity> updateProperty(@Context HttpServletRequest request,
            @PathParam("key") String key,
            @PathParam("value") String value,
            @PathParam("uuid") String uuid) throws Exception {
                                       ...
                             }

我必须修改它,所以它接受来自 REST 调用的不定(或多个)键值对列表,例如

@Path("property/{uuid}/{key1}/{value1}/{key2}/{value2}/{key3}/{value3}/...")

是否可以将它们存储在数组或列表中,所以我没有列出几十个@PathParams 和参数,以避免这种情况:

@PathParam("key1") String key1,
@PathParam("key2") String key2,
@PathParam("key3") String key3,
4

2 回答 2

6

可能是重新考虑这种设计的好机会。通过使用/s,我们在某种程度上表示,/我们试图找到不同的资源。键/值对(在 URL 的上下文中)主要用于查询参数或矩阵参数。

如果/property/{uuid}是主资源的路径,而我们只是想为客户端提供一些参数来访问该资源,那么我们可以允许矩阵参数或查询参数

矩阵参数(在请求 url 中)看起来像

/12345;key1=value1;key2=value2;key3=value3

获取值的资源方法可能类似于

@GET
@Path("/property/{uuid}")
public Response getMatrix(@PathParam("uuid") PathSegment pathSegment) {
    StringBuilder builder = new StringBuilder();

    // Get the {uuid} value
    System.out.println("Path: " + pathSegment.getPath());

    MultivaluedMap matrix = pathSegment.getMatrixParameters();
    for (Object key : matrix.keySet()) {
        builder.append(key).append(":")
               .append(matrix.getFirst(key)).append("\n");
    }
    return Response.ok(builder.toString()).build();
}

查询参数(在请求 url 中)可能类似于

/12345?key1=value1&key2=value2&key3=value3

获取值的资源方法可能类似于

@GET
@Path("/property/{uuid}")
public Response getQuery(@PathParam("uuid") String uuid, 
                         @Context UriInfo uriInfo) {

    MultivaluedMap params = uriInfo.getQueryParameters();
    StringBuilder builder = new StringBuilder();
    for (Object key : params.keySet()) {
        builder.append(key).append(":")
               .append(params.getFirst(key)).append("\n");
    }
    return Response.ok(builder.toString()).build();
}

不同的是,Matrix 参数可以嵌入到路径段中,而查询参数必须放在 URL 的末尾。您还可以注意到语法上的一些差异。


一些资源


更新

还查看PUT您的方法签名,您似乎正在尝试使用路径作为您尝试更新的值来更新资源,因为我在您的方法中没有看到实体主体的任何参数。放置时,您应该在实体主体中发送表示,而不是作为路径段参数。

于 2014-11-18T11:35:33.830 回答
4

一种解决方法:

@Path("/foo/bar/{other: .*}
public Response foo(@PathParam("other") VariableStrings vstrings) {

   String[] splitPath = vstrings.getSplitPath();


}

变量字符串类:

public class VariableStrings {

   private String[] splitPath;

   public VariableStrings(String unparsedPath) {
     splitPath = unparsedPath.split("/");
   }
}

JAX-RS/Jersey 中可变参数数组的路径段序列?

将可选参数映射到 Map 的另一个示例:

@GET
@ Produces({"application/xml", "application/json", "plain/text"})
@ Path("/location/{locationId}{path:.*}")
public Response getLocation(@PathParam("locationId") int locationId, @PathParam("path") String path) {
    Map < String, String > params = parsePath(path);
    String format = params.get("format");
    if ("xml".equals(format)) {
        String xml = "<location<</location<<id<</id<" + locationId + "";
        return Response.status(200).type("application/xml").entity(xml).build();
    } else if ("json".equals(format)) {
        String json = "{ 'location' : { 'id' : '" + locationId + "' } }";
        return Response.status(200).type("application/json").entity(json).build();
    } else {
        String text = "Location: id=" + locationId;
        return Response.status(200).type("text/plain").entity(text).build();
    }
}

private Map < String, String > parsePath(String path) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    String[] pathParts = path.split("/");
    Map < String, String > pathMap = new HashMap < String, String > ();
    for (int i = 0; i < pathParts.length / 2; i++) {
        String key = pathParts[2 * i];
        String value = pathParts[2 * i + 1];
        pathMap.put(key, value);
    }
    return pathMap;
}
于 2014-11-18T10:52:07.673 回答