1. @PathParam – Single Parameter
A simple and normal way to use @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("{employeeId}") public Response getEmployeeById(@PathParam("employeeId") String employeeId) { return Response.status(200).entity("getEmployeeById is called, employeeId : " + employeeId).build(); } }
In above example, the value of {employeeId} from “/employees/{employeeId}” will match to “@PathParam(“employeeId”) String var“.
URI Pattern : “/employees/12121″
getEmployeeById is called, employeeId : 12121
2. @PathParam – Multiple Parameters
Example to inject multiple parameters into Java method.
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("{year}/{month}/{day}") public Response getEmployeeHistory( @PathParam("year") int year, @PathParam("month") int month, @PathParam("day") int day) { String date = year + "/" + month + "/" + day; return Response.status(200) .entity("getEmployeeHistory is called, year/month/day : " + date) .build(); } }
URI Pattern : “/employees/2013/06/16”
getEmployeeHistory is called, year/month/day : 2013/06/16
Download SourceCode
JAX-RS @Path URI matching example.zip
References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service
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…