繁体   English   中英

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

[英]How to abort REST request inside a Jax-RS WriterInterceptor?

我在服务器上使用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。

该请求可以异常中止。 这是示例代码: ContentMD5Writer.java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM