Java Reference
In-Depth Information
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/meta")
public class MetaDataService {
@GET
public Response get() {
CacheControl cacheCtl = new CacheControl();
cacheCtl.setMaxAge(500);
cacheCtl.setMustRevalidate(true);
cacheCtl.setNoStore(true);
Calendar cal = new GregorianCalendar();
cal.roll(Calendar.YEAR, 1);
Date expy = cal.getTime();
Response response = Response.noContent()
.header("MY_KEY", "MY_VALUE")
.cacheControl(cacheCtl)
.expires(expy)
.language(Locale.ENGLISH)
.type(MediaType.TEXT_HTML)
.build();
return response;
}
}
You can append these builder methods in arbitrary order, with each method adding its own
metadata to the response. In this example, you specify a custom header of your own, set the
cache control to expire after 500 seconds, create a date representing one year in the future and
set that on your expires header, and so forth.
If you deploy the example and open a browser to http://localhost:8080/restexamples/re-
sources/meta , you won't see a response in the window (because you specified 204 No Con-
tent), but the headers produced by the response are as expected:
(Status-Line) HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Cache-Control: no-store, no-transform, must-revalidate, max-age=500
Expires: Sun, 08 Nov 2009 16:41:49 GMT
Search WWH ::




Custom Search