Red Hat DocumentationFuse ESBToggle FramesPrintFeedback

Getting the value of URI template variables

Overview

As described in Setting the path, resource paths can contain variable segments that are bound to values dynamically. Often these variable path segments are used as parameters to a resource method as described in Getting data from the URI's path. You can, however, also access them through the URI context.

Methods for getting the path parameters

The UriInfo interface provides two methods, shown in Example 48, that return a list of the path parameters.

Example 48. Methods for returning path parameters from the URI context

MultivaluedMap<java.lang.String, java.lang.String> getPathParameters();
MultivaluedMap<java.lang.String, java.lang.String> getPathParameters(boolean decode);

The getPathParameters() method that does not take any parameters automatically decodes the path parameters. If you want to disable URI decoding use getPathParameters(false).

The values are stored in the map using their template identifiers as keys. For example if the URI template for the resource is /{color}/box/{note} the returned map will have two entries with the keys color and note.

Example

Example 49 shows code for retrieving the path parameters using the URI context.

Example 49. Extracting path parameters from the URI context

import javax.ws.rs.Path;
import javax.ws.rs.Get;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.MultivaluedMap;

@Path("/monstersforhire/")
public class MonsterService

  @GET
  @Path("\{type}\{size}")
  public Monster getMonster(@Context UriInfo uri)
  {
    MultivaluedMap paramMap = uri.getPathParameters();
    String type = paramMap.getFirst("type");
    String size = paramMap.getFirst("size");
  }
}

Comments powered by Disqus