Store @PathParam values from REST call in a list or array

邮差的信 提交于 2019-11-29 02:42:32
Paul Samsotha

Might be a good opportunity to rethink this design. By using /s, we are in a way signifying, with each / that we are trying to locate a different resource. Key/Value pairs (in the context of the URL) are mainly for query parameters or matrix parameters.

If /property/{uuid} is the path to a main resource, and we just want to offer some parameters to the client for accessing this resource, then we could allow matrix parameters or query parameters

Matrix Parameters (in a request url) will look something like

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

A resource method to obtain the values might look something like

@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();
}

Query Parameters (in a request url) might look something like

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

A resource method to obtain the values might look something like

@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();
}

The difference is that Matrix parameters can be embedded into path segments, while query parameters must be placed at the end of the URL. You can also notice a little difference in syntax.


Some Resources


UPDATE

Also looking at the PUT in you method signature, it appears you are trying update a resource using the path as the values for which you are trying to update, as I don't see any parameters in your method for an entity body. When PUTting, you should be sending the representation in the the entity body, not as as path segments or parameters.

edubriguenti

A workaround:

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

   String[] splitPath = vstrings.getSplitPath();


}

VariableStrings class:

public class VariableStrings {

   private String[] splitPath;

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

Path segment sequence to vararg array in JAX-RS / Jersey?

Another example where you map the optional parameter to a 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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!