項目結構:
Maven父子級工程
引入依賴:
子項目引入Euraka server
父項目引入spring-bootstarter等等java
子工程Spring Boot項目啓動失敗,報以下錯誤:spring
*************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1321) The following method did not exist: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; The method's class, javax.servlet.ServletContext, is available from the following locations: jar:file:/C:/Users/xxx/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class jar:file:/C:/Users/xxx/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.35/tomcat-embed-core-9.0.35.jar!/javax/servlet/ServletContext.class It was loaded from the following location: file:/C:/Users/XXX/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar Action: Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext Process finished with exit code 0
重點在於最後這句: Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext
apache
Maven Build時正常,說明是運行時import的包有衝突。 網上不少人的說法是找衝突的包,而後刪掉重複便可。
但正常配置的項目不會有這樣的問題,根本緣由仍是配置。 api
經查,發現個人問題是,子項目的pom.xml中沒有配置正確的Spring Cloud版本。tomcat
錯誤的pom配置:👇直接將依賴的版本寫到dependency中 app
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.2.2.RELEASE</version> </dependency> </dependencies>
改成以下格式便可: 👇 指定spring cloud的version,由Spring cloud管理依賴的版本spring-boot
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
從新build,運行,發現運行良好。ui