我有一个Spring应用程序,其中一个依赖项是使用spring和一个嵌入式jetty来启动一个临时web服务器。这导致我的春季引导应用程序启动在一个码头,而不是一只猫。
我的春初网:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>
</exclusions>
</dependency>依赖项pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-http</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>是否有可能将服务器配置为通过spring引导显式地使用而不是由依赖树推断?
编辑
我进一步研究了这个问题,并创建了一个回购程序来重现这个问题:github.com/svettwer/spring-server-test。
org.eclipse.jetty.websocket:javax-websocket-server-impl使spring从jetty开始,而不需要任何其他配置。
编辑2
这个问题在SpringBoot2.x中不再存在。
编辑3我将删除前面提到的回购,但下面是导致问题的依赖设置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.7.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- Comment that in to start spring with jetty-->
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
</dependencies>发布于 2018-05-24 11:42:35
通常,如果您的类路径上有spring-boot-starter-tomcat (通过spring-boot-starter-web),它应该始终选择Tomcat,因为它比其他servlet容器具有优先级。即使您有以下依赖项,Spring引导也将从Tomcat开始:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</dependency>您可以通过注册自己的ServletWebServerFactory,以编程方式覆盖所选的servlet容器,例如:
@Bean
public ServletWebServerFactory factory() {
return new TomcatServletWebServerFactory();
}您可以选择预定义的TomcatServletWebServerFactory、JettyServletWebServerFactory或UndertowServletWebServerFactory。
发布于 2018-05-24 10:04:12
本指南可以帮助您:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html
如果运行mvn dependency:tree并搜索jetty,您可能会发现需要排除它,例如:
spring-boot-starter-jetty排除在示例中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>这是对码头的偏爱,而不是对猫的偏爱-但你希望你能.
希望这能有所帮助。
https://stackoverflow.com/questions/50506392
复制相似问题