2

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.

Cœur
  • 34,719
  • 24
  • 185
  • 251
Shubham Kumar
  • 157
  • 1
  • 2
  • 9

1 Answers1

2

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.

Community
  • 1
  • 1
cassiomolin
  • 113,708
  • 29
  • 249
  • 319