JAX-RS @Path URI Matching Example in Java
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.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WebServicesJAXRS</groupId>
<artifactId>WebServicesJAXRS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>WebServicesJAXRS</name>
<description>Restful Webservices</description>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now add Jersey servlet to our deployment descriptor web.xml as front controller.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.codenuclear</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Below is our JAX-RS Rest service class.
- Normal URI MatchingSee normal URI matching with @Path annotation.
package com.codenuclear;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/users")
public class RestServices {
@GET
public Response getUser() {
return Response.status(200).entity("getUser is called").build();
}
@GET
@Path("/vip")
public Response getUserVip() {
return Response.status(200).entity("getUserVip is called").build();
}
}

- URI Matching and ParameterThe value within an open brace “{” and close brace “}”, is represents a parameter, and can be access with @PathParam.
package com.codenuclear;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/users")
public class RestServices {
@GET
@Path("{name}")
public Response getUserByName(@PathParam("name") String name) {
return Response.status(200).entity("getUserByName is called, name : " + name).build();
}
}

- URI Matching and Regular Expression@Path support complex URI matching with regular expression, via following expression : {” variable-name [ “:” regular-expression ] “}
package com.codenuclear;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/users")
public class RestServices {
@GET
@Path("{id : \\d+}") //support digit only
public Response getUserById(@PathParam("id") String id) {
return Response.status(200).entity("getUserById is called, id : " + id).build();
}
@GET
@Path("/username/{username : [a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByUserName(@PathParam("username") String username) {
return Response.status(200).entity("getUserByUserName is called, username : " + username).build();
}
@GET
@Path("/books/{isbn : \\d+}")
public Response getUserBookByISBN(@PathParam("isbn") String isbn) {
return Response.status(200).entity("getUserBookByISBN is called, isbn : " + isbn).build();
}
}


