【发布时间】:2017-12-25 07:08:52
【问题描述】:
我目前正在使用 Restlets 框架,但找不到在服务方法中手动设置 HTTP 响应代码的方法。考虑下面的 sn-p 代码:
public class MyResource extends ServerResource {
@Post("json")
public Representation doSomething(Representation entity) throws IOException {
int status = 200;
try {
// do something which might throw an exception
}
catch (Exception e) {
// log the exception
// *** I would like to assign HTTP status 500 here ***
status = 500;
}
JSONObject responseJSON = new JSONObject();
responseJSON.put("result", "some data");
Representation rep = new JsonRepresentation(responseJSON.toJSONString());
return rep;
}
}
如果发生异常,我有能力捕获和记录异常,但不清楚如何更改 HTTP 响应代码。据我所知,从doSomething 返回将由带有 200 HTTP 响应代码的 Restlet 自动处理。
我知道如何直接从过滤器或 servlet 分配状态代码,但是否可以在 Restlets 中执行此操作,而无需进入 servlet 层?
【问题讨论】:
标签: java servlets http-status-codes restlet-2.0