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
  • 37,241
  • 25
  • 195
  • 267
Shubham Kumar
  • 167
  • 2
  • 3
  • 10

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
  • 124,154
  • 35
  • 280
  • 359