Java Reference
In-Depth Information
The NewCookie class extends the Cookie class discussed in Chapter 5 . To set response cook-
ies, create instances of NewCookie and pass them to the method ResponseBuild-
er.cookie() . For example:
@Path ( "/myservice" )
public
public class
class MyService
MyService {
@GET
public
public Response get () {
NewCookie cookie = new
new NewCookie ( "key" , "value" );
ResponseBuilder builder = Response . ok ( "hello" , "text/plain" );
return
return builder . cookie ( cookie ). build ();
}
Here, we're just setting a cookie named key to the value value .
The Status Enum
Generally, developers like to have constant variables represent raw strings or numeric values
within. For instance, instead of using a numeric constant to set a Response status code, you
may want a static final variable to represent a specific code. The JAX-RS specification
provides a Java enum called javax.ws.rs.core.Response.Status for this very purpose:
public
public enum
enum Status {
OK ( 200 , "OK" ),
CREATED ( 201 , "Created" ),
ACCEPTED ( 202 , "Accepted" ),
NO_CONTENT ( 204 , "No Content" ),
MOVED_PERMANENTLY ( 301 , "Moved Permanently" ),
SEE_OTHER ( 303 , "See Other" ),
NOT_MODIFIED ( 304 , "Not Modified" ),
TEMPORARY_REDIRECT ( 307 , "Temporary Redirect" ),
BAD_REQUEST ( 400 , "Bad Request" ),
UNAUTHORIZED ( 401 , "Unauthorized" ),
FORBIDDEN ( 403 , "Forbidden" ),
NOT_FOUND ( 404 , "Not Found" ),
NOT_ACCEPTABLE ( 406 , "Not Acceptable" ),
CONFLICT ( 409 , "Conflict" ),
GONE ( 410 , "Gone" ),
PRECONDITION_FAILED ( 412 , "Precondition Failed" ),
UNSUPPORTED_MEDIA_TYPE ( 415 , "Unsupported Media Type" ),
INTERNAL_SERVER_ERROR ( 500 , "Internal Server Error" ),
SERVICE_UNAVAILABLE ( 503 , "Service Unavailable" );
Search WWH ::




Custom Search