Java Reference
In-Depth Information
public
public abstract
abstract ResponseBuilder language ( String language );
public
public abstract
abstract ResponseBuilder language ( Locale language );
public
public abstract
abstract ResponseBuilder location ( URI location );
public
public abstract
abstract ResponseBuilder contentLocation ( URI location );
public
public abstract
abstract ResponseBuilder tag ( EntityTag tag );
public
public abstract
abstract ResponseBuilder tag ( String tag );
public
public abstract
abstract ResponseBuilder lastModified ( Date lastModified );
public
public abstract
abstract ResponseBuilder cacheControl ( CacheControl cacheControl );
public
public abstract
abstract ResponseBuilder expires ( Date expires );
public
public abstract
abstract ResponseBuilder header ( String name , Object value );
public
public abstract
abstract ResponseBuilder cookie ( NewCookie ... cookies );
}
As you can see, ResponseBuilder has a lot of helper methods for initializing various re-
sponse headers. I don't want to bore you with all the details, so check out the JAX-RS
Javadocs for an explanation of each one. I'll be giving examples using many of them
throughout the rest of this topic.
Now that we have a rough idea about creating custom responses, let's look at an example of
a JAX-RS resource method setting some specific response headers:
@Path ( "/textbook" )
public
public class
class TextBookService
TextBookService {
@GET
@Path ( "/restfuljava" )
@Produces ( "text/plain" )
public
public Response getBook () {
String book = ...;
ResponseBuilder builder = Response . ok ( book );
builder . language ( "fr" )
. header ( "Some-Header" , "some value" );
return
return builder . build ();
}
}
Here, our getBook() method is returning a plain-text string that represents a book our client
is interested in. We initialize the response body using the Response.ok() method. The status
Search WWH ::




Custom Search