java - 如何在没有 xml 的 Spring MVC 中启用 REST gzip 压缩?

标签 java spring rest spring-mvc gzip

那里有一些过滤器,但没有可用的纯 Java 解决方案或一些有用的库。我将 Spring MVC 与 Tomcat 一起使用并将发布部署到 Heroku(因此无法更改 servlet 容器配置)。如何在没有 XML 的情况下在 Spring MVC 中启用 REST gzip 压缩?

最佳答案

您可以设置在您的 servlet 容器上使用压缩的规则,例如您可以使用压缩属性的 apache-tomcat。来自 documentation :

compression

The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is "off" (disable compression), "on" (allow compression, which causes text data to be compressed), "force" (forces compression in all cases), or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If not specified, this attribute is set to "off".

Note: There is a tradeoff between using compression (saving your bandwidth) and using the sendfile feature (saving your CPU cycles). If the connector supports the sendfile feature, e.g. the NIO connector, using sendfile will take precedence over compression. The symptoms will be that static files greater that 48 Kb will be sent uncompressed. You can turn off sendfile by setting useSendfile attribute of the connector, as documented below, or change the sendfile usage threshold in the configuration of the DefaultServlet in the default conf/web.xml or in the web.xml of your web application.

compressionMinSize

If compression is set to "on" then this attribute may be used to specify the minimum amount of data before the output is compressed. If not specified, this attribute is defaults to "2048".

关于java - 如何在没有 xml 的 Spring MVC 中启用 REST gzip 压缩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24969577/

相关文章:

c# - 在 Base Controller 中实现依赖注入(inject)的最佳方式是什么

Java ImageIO - JPEG 黑色变为灰色

java - http转换为https进入无限循环

java - 在 XYDifference(渲染器)图下方创建面积图

Spring Boot Jersey 和监控 URL

java - Spring 批处理 : Writing data to multiple files with dynamic File Name

java - Spring Data REST 不包括资源中的实体链接

rest - RESTfully 公开对资源的操作 - 重载 POST、PUT 和 Controller 资源

java - 在 Spring 应用程序中将产品链接到订单的最佳方式是什么?

java - 在程序启动时将 main() 方法重定向到另一个方法有什么好处?