如何中止 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
我在我的服务器上使用 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