How to use gzip filters with REST API? Also, lets say I want to have a implementation at one place. Are there ways to configure out of like 20 APIs, only a few APIs use it.
Any documentation would be helpful.
How to use gzip filters with REST API? Also, lets say I want to have a implementation at one place. Are there ways to configure out of like 20 APIs, only a few APIs use it.
Any documentation would be helpful.
It could be achieved with a WriterInterceptor
:
public class GZIPWriterInterceptor implements WriterInterceptor {
@Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException, WebApplicationException {
final OutputStream outputStream = context.getOutputStream();
context.setOutputStream(new GZIPOutputStream(outputStream));
context.proceed();
}
}
Then register the WriterInterceptor
in your ResourceConfig
/ Application
subclass:
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(GZIPWriterInterceptor.class);
}
}
To bind the interceptor to certain resource methods or classes, you could use name binding annotations.
For further details, check the Jersey documentation about filters and interceptors.