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