Resteasy generally enable GZIP

為{幸葍}努か 提交于 2019-12-07 05:31:18

问题


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


回答1:


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.




回答2:


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() {
}



回答3:


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).




回答4:


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.



来源:https://stackoverflow.com/questions/17855815/resteasy-generally-enable-gzip

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