【发布时间】:2015-11-04 09:59:10
【问题描述】:
在 Jersey 中使用拦截器我可以操纵输出,但是,我还想在响应中添加一个标头,其值是根据输出结果计算得出的。
@Sha256Sum
public class Sha256SumInterceptor implements WriterInterceptor {
public static final String SHA256_HASH_HEADER_NAME = "SHA256-SUM";
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
// Retrieve the OutputStream, read the contents and calculate the hashsum.
// Set the header value in context.
context.proceed();
}
}
但是,问题在于,当我最终阅读了整个流时,我无法将标题设置为调用 context.proceed 并写入内容(从而使我能够对它做任何事情)我可以不再设置标题。
简而言之,我的问题是:如何将整个流输出捕获为 byte[],从字节数组计算结果,最后在对计算结果的响应中设置标头?我不想耗尽输出流。
【问题讨论】:
标签: java jersey jersey-2.0