如何在Jax-RS WriterInterceptor中中止REST请求?

时间:2016-06-02 13:17:21

标签: rest jboss jax-rs resteasy

我在服务器上使用javax.ws.rs.ext.WriterInterceptor,如下所示,在服务器发送到客户端之前修改服务器生成的REST响应。这是按预期工作的,但有没有办法从aroundWriteTo中止中断请求?

@Provider
@ServerInterceptor
public class MyInterceptor implements WriterInterceptor {

  @Override
  public void aroundWriteTo (WriterInterceptorContext context) {

    final ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();
    final OutputStream originalStream = context.getOutputStream();
    context.setOutputStream(bufferStream);
    context.proceed();

    final String originalMsg = bufferStream.toString(Charset.defaultCharset().name());
    try {
      // not important for this example what modify does
      final String modifiedMsg = modify(originalMsg);
    } catch (Exception e) {
      //How to abort request with HTTP status 500 at this point?
    }
    originalStream.write(modifiedMsg.getBytes(Charset.defaultCharset()));
    context.setOutputStream(originalStream);
  }
}

我使用的是JBoss EAS 6.4.7 GA,RestEasy 3.0.9。

1 个答案:

答案 0 :(得分:1)

请求可以通过例外中止。以下是示例代码:ContentMD5Writer.java