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