Resteasy generally enable GZIP

我怕爱的太早我们不能终老 提交于 2019-12-05 10:37:05

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!