Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My function looks like this:

    @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 {
                                       ...
                             }

I have to modify it, so it accepts indefinite(or many) list of key-value pairs from REST call, something like

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

Is it possible to store them in an array or list, so I do not list dozens of @PathParams and parameters, to avoid this:

@PathParam("key1") String key1,
@PathParam("key2") String key2,
@PathParam("key3") String key3,
like image 834
gmode Avatar asked Dec 25 '22 02:12

gmode


1 Answers

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();
}
  • See PathSegment

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();
}
  • See UriInfo

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

  • Query String (Wikipedia)
  • When to use query parameters versus matrix parameters?
  • URL matrix parameters vs. request parameters

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.

like image 77
Paul Samsotha Avatar answered Dec 28 '22 09:12

Paul Samsotha