Frage

I have a RestEasy + Java EE application. When I add @GZIP to a component class, the server-answer is gzipped, if the client sends "accepts:gzip"

Is there a way to generally enable gzip for all components? I don't like to add the annotation to every class.

I'm using RestEasy JAX-RS 3.0.1

War es hilfreich?

Lösung 2

No, there is no way with annotations to enable gzip for all resources. If you wanted to forego adding the annotation to every class you could create a servlet filter that looks at the incoming headers and gzips the response on the way out.

Andere Tipps

if you are implementing your API behind an interface, so all your interfaces might inherit from one interface let us name is "BaseAPI" and logically if you set @Gzip on the BaseAPI so it would apply Content-Encoding for all inherited interfaces and method.

@GZIP
public interface BaseAPI
{
}


public interface APIX extends BaseAPI
{
   @GET
   Response getSomething() {
}

You can do this with custom JAX-RS 2.0 filters and interceptors, and it's not even particularly hard once you know how.

What you'll need to do is add a filter that modifies the existing ones for GZIP so it does not check for the annotation to be present to support the encoding, it only looks for the Accept-Encoding header.

Look at how RestEasy GZIP encoding is implemented: https://github.com/resteasy/Resteasy/tree/master/jaxrs/resteasy-jaxrs/src/main/java/org/jboss/resteasy/plugins/interceptors/encoding

You'd need to add Features that have method configure(ResourceInfo resourceInfo, FeatureContext configurable) which always add the GZIP filters, regardless of annotations present. You'll need one Feature that registers a custom Filter for Server, and one for Client.

With those in place, the pre-existing GZIP interceptors should do the rest of the work.

I've used similar mechanisms to create a custom compression filter (although I ended up setting it up to be applied by annotation to limit scope).

Use Apache for this. Apache can handle it automatically and optimize (gzip in your case) all your responses to clients. It will not only gzip it, but also attach all necessary response headers, that allow clients identify, that content is zippid to let them unzip it.

No changes in the code is needed for this issue.

Reasteasy has this GZIPDecodingInterceptor. So, you can do this when you create the client:

import org.jboss.resteasy.plugins.interceptors.GZIPDecodingInterceptor;
import org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl;

ResteasyClient client = new ResteasyClientBuilderImpl().build();
client.register(GZIPDecodingInterceptor.class);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top