...
Code Block |
---|
@Path("/example")
public class RootResource {
@GET
public Response invokeWithParameters(@QueryParam("q") String searchTerm) {
if(q == null) {
return Response.status(Status.BAD_REQUEST).build();
}
/* do a search */
return Response.ok(/* some entity here */).build();
}
}
|
If a HTTP GET request to "/example?q=abcd" is made, searchTerm would have "abcd" as the value when invoked.
Anchor |
---|
| PathParameter |
---|
| PathParameter |
---|
|
Path Parameters (@PathParam)
Path parameters take the incoming URL and match parts of the path as a parameter. By including {name} in a @Path annotation, the resource can later access the matching part of the URI to a path parameter with the corresponding "name". Path parameters make parts of the request URL as parameters which can be useful in embedding request parameter information to a simple URL.
Code Block |
---|
@Path("/books/{bookid}")
public class BookResource {
@GET
public Response invokeWithBookId(@PathParam("bookid") String bookId) {
/* get the info for bookId */
return Response.ok(/* some entity here */).build();
}
@GET
@Path("{language}")
public Response invokeWithBookIdAndLanguage(@PathParam("bookid") String bookId, @PathParam("language") String language) {
/* get the info for bookId */
return Response.ok(/* some entity here */).build();
}
}
|
In the above, HTTP GET to /books/1 or to /books/abcd would result in invokeWithBookId being called. If a HTTP GET was issued to /books/1/en or /books/1/fr or /books/abcd/jp, then invokeWithBookIdAndLanguage would be invoked with the appropriate parameters.
Anchor |
---|
| MatrixParameter |
---|
| MatrixParameter |
---|
|
...
Code Block |
---|
@Path("/")
public class RootResource {
@GET
public Response invokeWithParameters(@MatrixParam("name") String name) {
if(name == null) {
return Response.status(Status.BAD_REQUEST).build();
}
return Response.ok(/* some entity here */).build();
}
}
|
Anchor |
---|
| HeaderParameter |
---|
| HeaderParameter |
---|
|
Header parameters are useful especially when there are additional metadata control parameters that need to be passed in (i.e. security information, cache information, and so forth).
Code Block |
---|
@Path("/")
public class RootResource {
@GET
public Response invokeWithParameters(@HeaderParam("controlInfo") String searchTermcontrolInfo) {
if(qcontrolInfo == null) {
return Response.status(Status.BAD_REQUEST).build();
}
return Response.ok(/* dosome aentity here */).build();
}
}
|
Anchor |
---|
| CookieParameter |
---|
| CookieParameter |
---|
|
CookieParameters (@CookieParam)
In a REST application, requests are stateless but sometimes applications use Cookies for various reasons (i.e. put login information without embedding it into the URL). CookieParam is used to easily get the information.
Code Block |
---|
@Path("/")
public class RootResource {
@GET
public Response invokeWithParameters(@CookieParam("userId") String userId) {
if(userId == null) {
return Response.status(Status.BAD_REQUEST).build();
}search */
return Response.ok(/* some entity here */).build();
}
}
|