4

我似乎无法弄清楚如何将标题添加到我的 restlet 响应中。当我查看Response对象中的可用方法时,我看到的只是setStatus, setEntitysetAttributes但是这些都没有告诉我如何在响应中设置自定义 http 标头。

例如,我有一个GET调用,返回类似于以下内容:

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",
  }
}

不管它可能。在handleGet方法中,我是这样处理的:

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

@Override
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.setStatus(Status.SUCCESS_OK);
    res.setEntity(docStr, textJsonType);

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

1 回答 1

10

因为 Restlet 更多的是关于 REST 架构原则而不是 HTTP,所以它试图与协议无关并且不直接公开 HTTP 标头。但是,它们存储在org.restlet.http.headers响应的属性中(作为 a Form)。请注意,您只能以这种方式设置自定义标题,而不是标准标题(这些由框架直接处理,例如Content-Type取决于Representation's MediaType)。

请参阅此示例: http ://blog.arc90.com/2008/09/15/custom-http-response-headers-with-restlet/ (链接内容也可从Internet Archive Wayback Machine获得)。

于 2010-09-17T00:14:54.960 回答