如何在JAX-RS中实现WriterInterceptor接口时访问请求头?
context.getHeaders(); //This line gives a set of response headers(not request headers) in the WriterInterceptor implementation.
Run Code Online (Sandbox Code Playgroud)
完整代码如下:
public class GzipFilterWriterInterceptor implements WriterInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(GzipFilterWriterInterceptor.class);
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
MultivaluedMap<String,Object> headers = context.getHeaders();
headers.add("Content-Encoding", "gzip");
final OutputStream outputStream = context.getOutputStream();
context.setOutputStream(new GZIPOutputStream(outputStream));
context.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)