使用带有RESTful的Spring Boot / MVC / JavaConfig进行GZIP压缩 [英] Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful

查看:184
本文介绍了使用带有RESTful的Spring Boot / MVC / JavaConfig进行GZIP压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将Spring Boot / MVC与基于注释的java-config一起用于一系列 RESTful 服务,我们希望有选择地启用 HTTP GZIP 对某些API响应进行流压缩。

We use Spring Boot/MVC with annotation-based java-config for series of RESTful services and we want to selectively enable HTTP GZIP stream compression on some API responses.

我知道我可以在我的控制器中手动执行此操作并使用 byte [] @ResponseBody ,但是我们更喜欢依赖SpringMVC基础设施(过滤器/等)并让它自动进行JSON转换和压缩(即该方法返回一个POJO)。

I know I can do this manually in my controller and a byte[] @ResponseBody, however we'd prefer to rely on the SpringMVC infrastructure (filters/etc) and have it automatically do the JSON conversion and compression (ie the method returns a POJO).

如何在ResponseBody或嵌入式Tomcat实例中启用GZIP压缩,并且我们可以选择性地仅压缩一些响应?

How can I enable GZIP compression in the ResponseBody or embedded Tomcat instance, and in a way we can selectively compress only some responses?

谢谢!

PS。:我们目前没有任何基于XML的配置。

PS.: We don't currently have any XML based configuration.

推荐答案

这些答案的其余部分已经过时和/或顶部复杂,应该是简单的IMO(gzip现在已经存在了多长时间?比Java更长...)来自文档:

在application.properties 1.3+

In application.properties 1.3+

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css

在application.properties中 1.2.2 - < 1.3

In application.properties 1.2.2 - <1.3

server.tomcat.compression: on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css

小于1.2.2:

@Component
public class TomcatCustomizer implements TomcatConnectorCustomizer {

  @Override
  public void customize(Connector connector) {
    connector.setProperty("compression", "on");
    // Add json and xml mime types, as they're not in the mimetype list by default
    connector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,application/json,application/xml");
  }
}



另请注意,只有在运行嵌入式设备时这才有效tomcat:



如果您计划部署到非嵌入式tomcat,则必须在server.xml http://tomcat.apache.org/tomcat-9.0-doc/config/http.html#Standard_Implementation

另外要避免所有这些考虑在前面使用代理/负载均衡器设置Tomcat与 nginx 和/或 haproxy 或类似的,因为它将比Java / Tomcat的线程模型更有效,更轻松地处理静态资产和gzip MUCH。

Also to avoid all of this consider using a proxy/load balancer setup in front of Tomcat with nginx and/or haproxy or similar since it will handle static assets and gzip MUCH more efficiently and easily than Java/Tomcat's threading model.

你不想把'cat in the bath因为它正在忙着压缩东西而不是提供请求(或者更有可能在线程/吃CPU /堆等待数据库IO到运行时发生根据你正在做的事情,传统的Java / Tomcat开始时可能不是一个好主意,但我离题了......)

You don't want to throw 'cat in the bath because it's busy compressing stuff instead of serving up requests (or more likely spinning up threads/eating CPU/heap sitting around waiting for database IO to occur while running up your AWS bill which is why traditional Java/Tomcat might not be a good idea to begin with depending on what you are doing but I digress...)

refs :
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#how-to-enable-http-response-compression

https://github.com/spring -projects / spring-boot / issues / 2031

这篇关于使用带有RESTful的Spring Boot / MVC / JavaConfig进行GZIP压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆