The @Path annotation identifies the URI path template to which the resource responds, and is specified at the class level of a resource. The @Path annotation’s value is a partial URI path template relative to the base URI of the server on which the resource is deployed, the context root of the WAR, and the URL pattern to which the Jersey helper servlet responds.

URI path templates are URIs with variables embedded within the URI syntax. These variables are substituted at runtime in order for a resource to respond to a request based on the substituted URI. Variables are denoted by curly braces.

In JAX-RS, you can use @Path to bind URI pattern to a Java method. See following examples to show you how it works.

Here is the final pom.xml file having required dependencies.

Now add Jersey servlet to our deployment descriptor web.xml as front controller.

Below is our JAX-RS Rest service class.

  1. Normal URI Matching

    See normal URI matching with @Path annotation.

  2. Output

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/vip

  3. URI Matching and Parameter

    The value within an open brace “{” and close brace “}”, is represents a parameter, and can be access with @PathParam.

  4. Output

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/codeNuclear

  5. URI Matching and Regular Expression

    @Path support complex URI matching with regular expression, via following expression :

    {” variable-name [ “:” regular-expression ] “}

  6. Output

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/410

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/username/i4

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/books/8989

4 Thoughts on “JAX-RS @Path URI Matching Example in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *