REST

JAX-RS @Path URI matching example

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

1. Normal URI Matching
See normal URI matching with @Path annotation

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

URI pattern : “/employees”

getEmployee is called

URI pattern : “/employees/manager”

getEmployeeManageris is called

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

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

URI Pattern : “/employees/dinesh”

getEmployeeByName is called, name : dinesh

URI Pattern : “/employees/sweety”

getEmployeeByName is called, name : sweety

3. URI Matching and Regular Expression
@Path support complex URI matching with regular expression, via following expression :

{" variable-name [ ":" regular-expression ] "}
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();

}

}

URI Pattern : “/employees/1212”

getEmployeeByEmpId is called, employeeId : 1212

URI Pattern : “/employees/123456”

getEmployeeByEmpId is called, employeeId : 123456

URI Pattern : “/employees/employeeName/ddddd” , failed, don’t match “[a-zA-Z][a-zA-Z_0-9]”, first character need “[a-zA-Z]”, second character need “[a-zA-Z_0-9]”.

Could not find resource for relative : /employees/employeeName/ddddd

URI Pattern : “/employees/employeeName/d5”

getEmployeeByEmployeeName is called, employeeName : d5

URI Pattern : “employees/salary/1212”

getEmployeeSalaryBySapId is called, sapid: 1212

Download SourceCode
JAX-RS @Path URI matching example.zip

References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service

 

<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago