我正在使用 Restlet 2.0.15 版
我正在实现一项服务,该服务会动态生成一个可能很大的 zip 文件,以供 ReST 客户端下载。在生成这个文件时,可能会发生错误,当它发生时,我想给客户端一个有意义的错误响应。
我的设置包括:
In the main, an instance of Application is constructed, and the Component.setStatusService(StatusService) and
Application.setStatusSevice(StatusService) are invoked with MyStatusService (Extends StatusService)
and, and MyStatusService overrides:
public Representation getRepresentation(Status status, Request request, Response response) {
public Status getStatus(Throwable throwable, Request request, Response response) {
public Status getStatus(Throwable throwable, UniformResource resource) {
...
When I handle the GET request from the client, I set the entity in the response
(Response.setEntity(Representation) with MyRepresentation which extends OutputRepresentation.
The MyRepresentation class overrides the method:
public void write(WritableByteChannel writableChannel)
which calls the overridden method:
public void write(OutputStream outputStream)
which does the work of generating the zip file.
当我运行它时,会发生以下情况:
ServerCall.sendResponse(Response) 调用 -->
ServerCall.writeResponseBody(Representation, WritableChannel, OutputStream) --> 调用
MyRepresentation(扩展 OutputRepresentation).write(OutputStream)
它发现了一个问题(如预期的那样)(出于测试原因故意不好)请求,并希望给客户端一个有意义的响应。所以它抛出一个异常(应该是 ResourceException,还是任何异常?),它被捕获在
ServerAdapter.commit(HttpResponse)
但是在这之后,没有调用 MyStatusService 中的任何方法,所以客户端没有得到有意义的消息,只有 500 状态。
有没有办法控制状态(最好是给客户一个信息性消息)?
感谢您提供任何信息,
- 吉姆