Path Params in GET Rest Request
In JAX-RS, you can use @PathParam
annotation to extract the parameter from the request URI and map it to any method.
Suppose the client wants the information for student with the roll number 1 or 2 and not for all the students. In such situation, we can use the @PathParam
to map this to a method which will return only the requested student's record.
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/{rollno}")
public Student getStudentWithRollNo(@PathParam("rollno")int rollNo)
{
Student student = studentService.getStudentWithRollNo(rollNo);
return student;
}
The @PathParam
annotation will map the rollno from the resource URI to the rollNo argument of the method.
Thus with the @PathParam
annotation we can map the request URI parameters with the Java method arguments.