<div dir="ltr" style="text-align: justify;" trbidi="on">In this <b>JAX-RS</b> tutorial , you can use <b>@Path</b> to bind <b>URI </b>pattern to a Java method. See following examples to show you how it works.</p>
<p> <b>1. Normal URI Matching</b><br />
See normal URI matching with <b>@Path</b> annotation</p>
<pre class="highlight" name="code">import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.core.Response; 
 
@Path("/employees") 
public class EmployeeRestService { 
 @GET 
 public Response getEmployee() { 
 return Response.status(200).entity("getEmployee is called").build(); 
 } 
 @GET 
 @Path("/manager") 
 public Response getEmployeeManager() { 
 return Response.status(200).entity("getEmployeeManageris called").build(); 
 } 
} 
</pre>
<div align="center" id="ads-id"></div>
<p><i><b>URI pattern : &#8220;/employees&#8221;</b></i></p>
<p><b>getEmployee is called</b><br />
<b><br />
</b> <i><b>URI pattern : &#8220;/employees/manager&#8221;</b></i></p>
<p><b>getEmployeeManageris is called</b></p>
<p><b>2. URI Matching and Parameter</b><br />
The value within an open brace <b>&#8220;{&#8221; and close brace &#8220;}&#8221;</b>, is represents a parameter, and can be access with <b>@PathParam.</b></p>
<pre class="highlight" name="code">import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.core.Response; 
 
@Path("/employees") 
public class EmployeeRestService { 
 
 @GET 
 @Path("{employeeName}") 
 public Response getEmployeeByName(@PathParam("employeeName") String employeeName) { 
 
 return Response.status(200) 
 .entity("getEmployeeByName is called, employee name: " + employeeName).build(); 
 
 } 
 } 
</pre>
<p>
<i><b>URI Pattern : &#8220;/employees/dinesh&#8221;</b></i></p>
<p><b>getEmployeeByName is called, name : dinesh</b></p>
<p><i><b>URI Pattern : &#8220;/employees/sweety&#8221;</b></i><br />
<b><br />
</b> <b>getEmployeeByName is called, name : sweety</b></p>
<p> <b>3. URI Matching and Regular Expression</b><br />
<b>@Path</b> support complex URI matching with regular expression, via following expression :</p>
<pre class="highlight" name="code">{" variable-name [ ":" regular-expression ] "} 
</pre>
<pre class="highlight" name="code">import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.core.Response; 
 
@Path("/employees") 
public class EmployeeRestService { 
 
@GET 
@Path("{employeeId : d+}") //support digit only 
public Response getEmployeeByEmpId(@PathParam("employeeId") String employeeId) { 
 
return Response.status(200).entity("getEmployeeByEmpId is called, employeeId : " + employeeId).build(); 
 
} 
 
@GET 
@Path("/employeeName/{employeeName : [a-zA-Z][a-zA-Z_0-9]}") 
public Response getEmployeeByEmployeeName(@PathParam("employeeName") String employeeName) { 
 
return Response.status(200) 
.entity("getEmployeeByEmployeeName is called, employeeName : " + employeeName).build(); 
 
} 
 
@GET 
@Path("/salary/{sapid : d+}") 
public Response getEmployeeSalaryBySapId(@PathParam("sapid") String sapid) { 
 
return Response.status(200) 
.entity("getEmployeeSalaryBySapId is called, sapid : " + sapid).build(); 
 
} 
 
} 
</pre>
<p><i><b>URI Pattern : &#8220;/employees/1212&#8221;</b></i></p>
<p> getEmployeeByEmpId is called, employeeId : 1212</p>
<p> <i><b>URI Pattern : &#8220;/employees/123456&#8221;</b></i></p>
<p> <b>getEmployeeByEmpId is called, employeeId : 123456</b></p>
<p> URI Pattern :<i><b> &#8220;/employees/employeeName/ddddd&#8221;</b></i> , failed, don’t match &#8220;[a-zA-Z][a-zA-Z_0-9]&#8221;, first character need &#8220;[a-zA-Z]&#8221;, second character need &#8220;[a-zA-Z_0-9]&#8221;.</p>
<p> <b>Could not find resource for relative : /employees/employeeName/ddddd</b></p>
<p> <i><b>URI Pattern : &#8220;/employees/employeeName/d5&#8221;</b></i></p>
<p> <b>getEmployeeByEmployeeName is called, employeeName : d5</b><br />
<i><b><br />
</b></i> <i><b>URI Pattern : &#8220;employees/salary/1212&#8221;</b></i></p>
<p> <b>getEmployeeSalaryBySapId is called, sapid: 1212</b></p>
<p><b>Download SourceCode</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/JAX-RS%20%40Path%20URI%20matching%20example.zip?attredirects=0&;d=1" target="_blank">JAX-RS @Path URI matching example.zip</a></b></p>
<p><i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank">Wikipedia for REST Web Service</a></b></i></p>
<p> ; </p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/using-jax-rs-with-jaxb/">Previous </a><;<; ; ; || <a href="https://dineshonjava.com/jax-rs-web-service-tutorial/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/jax-rs-pathparam-example/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/using-jax-rs-with-jaxb/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/jax-rs-pathparam-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '424'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…