Parameters
Parameters can be are used to pass in and add additional information to a request. Search queries, offsets/pages in a collection, and other information can be used. While parameters are sometimes used to add more verbs to HTTP, it is advised not to use parameters as a way to create new HTTP methods or to make existing HTTP methods break the generally understood attributes (i.e. idempotent).
Parameters can be any basic Java primitive type including java.lang.String as well as types with a constructor that takes in a single String or a valueOf(String) static method. AlsoIn addition, List, SortedSet, and Set can be used where the generic type is one of the previously mentioned types (i.e. such as a Set<String>) when a parameter can have multiple values.
When full control is needed for parsing requests, it is generally recommend that developers use a String as the parameter type so that some basic inspection can be done performed and developers can customize error path responses.
...
http://www.google.com/search?q=apache+wink
http://www.example.com/users?offset=100&numPerPage=20
In JAX-RS to read query parameters, add a parameter to the resource method and annotate with @QueryParam:
...
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 aboveprevious example, HTTP GET to /books/1 or to /books/abcd would result in invokeWithBookId being called. If a HTTP GET was request is issued to /books/1/en or /books/1/fr or /books/abcd/jp, then invokeWithBookIdAndLanguage would be invoked with the appropriate parameters.
...
Header parameters are useful especially when there are additional metadata control parameters that need to be passed in (i.e. for example, security information, cache information, and so forth).
Code Block |
---|
@Path("/") public class RootResource { @GET public Response invokeWithParameters(@HeaderParam("controlInfo") String controlInfo) { if(controlInfo == null) { return Response.status(Status.BAD_REQUEST).build(); } return Response.ok(/* some entity here */).build(); } } |
...