我似乎无法弄清楚如何将标题添加到我的 restlet 响应中。当我查看Response
对象中的可用方法时,我看到的只是setStatus
, setEntity
,setAttributes
但是这些都没有告诉我如何在响应中设置自定义 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()));
}
}