【发布时间】:2016-05-03 06:36:26
【问题描述】:
我有兴趣为基于 Java 8 / spring-boot 1.3.2 / Maven 3.3 的应用程序启用 HTTP 响应压缩。
根据http://docs.spring.io/spring-boot/docs/1.3.x/reference/htmlsingle/#how-to-enable-http-response-compression 的 spring-boot 参考文档,我在 application.properties 中添加了以下更改:
server.compression.enabled=true
虽然应用程序在上述添加之前成功启动,但在使用以下 Maven 命令时应用程序无法启动并出现以下错误:
mvn clean spring-boot:run
...
...
2016-01-26 09:48:48.802 ERROR 15204 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.IllegalStateException: Compression is enabled, but GzipHandler is not on the classpath
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at com.miletwentyfour.client.Application.main(Application.java:43) [classes/:na]
Caused by: java.lang.IllegalStateException: Compression is enabled, but GzipHandler is not on the classpath
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.createGzipHandler(JettyEmbeddedServletContainerFactory.java:192) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.addHandlerWrappers(JettyEmbeddedServletContainerFactory.java:168) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:148) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:160) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
... 8 common frames omitted
根据参考文档第 64.18.2 节,我的下一步是将以下依赖项添加到 pom.xml:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.3.7.v20160115</version>
</dependency>
使用 mvn:dependency:tree 我可以验证 jetty-servlets 作为编译时范围的依赖项包含在应用程序中。然而,应用程序无法启动,并出现与上述相同的错误输出。
在从 Maven 命令行启动应用程序时,谁能指出我缺少什么和/或我需要做什么才能使响应压缩成功工作?
【问题讨论】:
-
您正在使用 Spring Boot 1.2 和 1.3 中的新属性。升级到 Spring Boot 1.3.x 应该可以解决您的问题
-
对不起,我错误地指认了我正在使用的 spring-boot 版本:我实际上使用的是 1.3.2,而不是 1.2.3。我已经更新了原始帖子以纠正错字。
-
看起来您正在混合 Jetty 模块的版本。 Spring Boot 1.3 使用 9.2.x,使用该版本,
spring-boot-starter-jetty应该为您提供所需的所有依赖项。 -
我已经验证你是正确的。虽然我很奇怪 Eclipse(和 mvn 依赖项:树)告诉我,当我添加最新的 9.2 版本的 jetty-servlets(9.2.14.v20151106)时,它说我正在手动覆盖已经在使用的版本。但是,如果我没有在 pom.xml 中明确声明它,则永远找不到所需的类,并且应用程序无法启动。
标签: java spring maven spring-mvc spring-boot