Java Reference
In-Depth Information
@CookieParam
Servers can store state information in cookies on the client, and can retrieve that information
when the client makes its next request. Many web applications use cookies to set up a ses-
sion between the client and the server. They also use cookies to remember identity and user
preferences between requests. These cookie values are transmitted back and forth between
the client and server via cookie headers.
The
@javax.ws.rs.CookieParam
annotation allows you to inject cookies sent by a client re-
quest into your JAX-RS resource methods. For example, let's say our applications push a
customerId
cookie to our clients so that we can track users as they invoke and interact with
our web services. Code to pull in this information might look like this:
@Path
(
"/myservice"
)
public
public class
class
MyService
MyService
{
@GET
@Produces
(
"text/html"
)
public
public
String
get
(
@CookieParam
(
"customerId"
)
int
custId
) {
...
}
}
The use of
@CookieParam
here makes the JAX-RS provider search all cookie headers for the
customerId
cookie value. It then converts it into an
int
and injects it into the
custId
para-
meter.
If you need more information about the cookie other than its base value, you can instead in-
ject a
javax.ws.rs.core.Cookie
object:
@Path
(
"/myservice"
)
public
public class
class
MyService
MyService
{
@GET
@Produces
(
"text/html"
)
public
public
String
get
(
@CookieParam
(
"customerId"
)
Cookie custId
) {
...
}
}
The
Cookie
class has additional contextual information about the cookie beyond its name
and value:
package
package
javax
.
ws
.
rs
.
core
;