Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

HTTP Headers

HTTP headers store mainly store metadata and control information. There are some common headers shared in requests and responses but there are a few specific to either a request or a response. Developers should read the HTTP spec specification for a complete understanding of HTTP headers. Some of the more common HTTP headers are mentioned below in cases where JAX-RS provides convenient methods for the header.

Generally, in order to get the request header name and values, developers can use either an injected @HeaderParam annotated with a parameter/field/property or an injected @Context HttpHeaders parameter/field/property.

Code Block
@Path("/example")
public ExampleResource {
    @Context HttpHeaders requestHeaders;

    @GET
    public void getSomething(@HeaderParam("User-Agent") String theUserAgent) {
        /* use theUserAgent variable or requestHeader's methods to get more info */
    }
}

To In order to set response headers, developers can set them on a javax.ws.rs.core.Response return object.

Code Block
@Path("/example")
public ExampleResource {
    @GET
    public Response getSomething() {
        return Response.ok(/* some entity */).header("CustomHeader", "CustomValue").build();
    }
}

Also, A response headers can be set when a MessageBodyWriter#writeTo() method is called.

...

The Content-Type signals the media type of the request/response entity. The Content-Type header on requests can be is read via HttpHeaders#getMediaType() method. The Content-Type is set for responses when doing a javax.ws.rs.core.Response.ResponseBuilder#type() method or a javax.ws.rs.core.Response.ResponseBuilder#variant() method.

Content-Language

The Content-Language denotes what language the entity is in. To get In order to receive the request entity language, use the HttpHeaders#getLanguage() method. To In order to set the response entity language, use the javax.ws.rs.core.Response.ResponseBuilder#language() method.

Content-Length

The Content-Length is useful for determining the entity's length. If possible, the MessageBodyWriter entity providers will automatically set the Content-Length if possible, and some containers will set the response Content-Length if the entity is small. See

Info
titleReference

Refer to the HTTP spec for more details on when this header is set and valid.

Anchor
RequestHeaders
RequestHeaders

...

The Accept header is used to determine the possible response representations that the client prefers (i.e. such as XML over JSON but not plain text). When a resource method is effectively annotated with a @Produces, any incoming request must have a compatible Accept header value for the resource method to be selected. Clients can set quality parameters (priority ordering) for the best possible response and services generally try to honor the request. To get the best representation of a response, use either the HttpHeaders#getAcceptableMediaTypes() or Request#selectVariant() methods.

...

Like the Accept header, Accept-Language lists the preferred languages. HttpHeaders#getAcceptableLanguages() method will list the acceptable languages in a preferred order.

...

If a previous response had an ETag header, the client can re-use the ETag value with the If-Match or If-None-Match request header to do conditional requests (if the server application supported the If-Match/If-None-Match headers). For instanceexample, a POST with an If-Match header and an old ETag value should only execute the POST request if the old ETag value is still valid. javax.ws.rs.core.Request#evaluatePreconditions() may help evaluate the If-Match and If-None-Match headers.

...

Common Response HTTP Headers

HTTP Headers form the core of an HTTP request, and are very important in an HTTP response. They define various characteristics of the data that is requested or the data that has been provided. The headers are separated from the request or response body by a blank line

ETag

ETags or entity tags can be set. Like a hashcode, it is given to the client so a client can use various control request headers such as If-Match and If-None-Match for conditional requests. javax.ws.rs.core.Response.ResponseBuilder#tag() and javax.ws.rs.core.EntityTag are useful for ETags.

...

The Expires response header indicates for how long the amount of time that the response entity should be cached. It is useful to set the expiration for data that is not going to change for a known time length. Browsers use this response header to manage their caches among other user agents.The javax.ws.rs.core.Response.ResponseBuilder#expires() method can be used to set the Expires header.

...

Last-Modified specifies the date when the resource was changed. A client can use the response value in combination with If-Modified-Since and If-Unmodified-Since request headers to perform conditional requests.The javax.ws.rs.core.Response.ResponseBuilder#lastModified() method can be used to set the Last-Modified header.

Info
titleImportant Note

Note that date formats are specified in the HTTP

...

specification.

Location

The Location response header usually indicates where the resource is located (in a redirect) or the URI of the new resource (when resources are created and usually in a HTTP 201 response). The javax.ws.rs.core.Response.ResponseBuilder#location()method can be used to set the Location header.