@QueryParam
can be conveniently used with the Default Value annotation so that you can avoid a null pointer exception if no query parameter is passed.When you want to parse query parameters from a GET request, you can simply define respective parameter to the method that will handle the GET request and annotate them with @QueryParam
annotation
@PathParam
extracts the URI values and matches to @Path
. And hence gets the input parameter.
2.1 @PathParam
can be more than one and is set to methods arguments
@Path("/rest")
public class Abc {
@GET
@Path("/msg/{p0}/{p1}")
@Produces("text/plain")
public String add(@PathParam("p0") Integer param1, @PathParam("p1") Integer param2 )
{
return String.valueOf(param1+param2);
}
}
In the above example,
http://localhost:8080/Restr/rest/msg/{p0}/{p1}
,
p0
matches param1
and p1
matches param2
. So for the URI
http://localhost:8080/Restr/rest/msg/4/6
,
we get the result 10
.
In REST Service, JAX-RS provides @QueryParam
and @FormParam
both for accepting data from HTTP request. An HTTP form can be submitted by different methods like GET and POST.
@QueryParam
: Accepts GET request and reads data from query string.
@FormParam
: Accepts POST request and fetches data from HTML form or any request of the media