Mohamed Nuur Mohamed Nuur - 1 year ago 138
Java Question

In Java, how to set the header of a Restlet Response?

I can't seem to figure out how to add headers to my restlet response. When I look at the available methods in the

object, all I see is
, and
but none of these tell me how to set custom http headers on the response.

For example, I have a
call the returns something like the following:

HTTP/1.1 200 OK
Content-Type: text/json
Content-Length: 123
Some-Header: the value
Some-Other-Header: another value

id: 111,
value: "some value this could be anything",
diagnosis: {
start: 12552255,
end: 12552261,
key: "ABC123E11",
source: "S1",

Whatever it maybe. In the
method, I handle it like so:

final MediaType textJsonType = new MediaType("text/json");

public void handleGet() {
log.debug("Handling GET...");
final Response res = this.getResponse();

try {
final MyObject doc = this.getObj("hello", 1, "ABC123E11", "S1");
final String docStr = doc.toString();

res.setEntity(docStr, textJsonType);

// need to set Some-header, and Some-other-header here!
catch(Throwable t) {
res.setEntity(new TextRepresentation(t.toString()));


Because Restlet is more about the REST architectural principles than HTTP, it tries to be protocol agnostic and doesn't expose the HTTP headers directly. However, they are stored in the org.restlet.http.headers attribute of the response (as a Form). Note that you can only set custom headers this way, not standard ones (these are handled directly by the framework, e.g. Content-Type depends on the Representation's MediaType).

See this for an example: (link content also available from the Internet Archive Wayback Machine).