我正在尝试构建一个SpringBoot应用程序。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class App {
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
ApplicationContext ctx = SpringApplication.run(App.class, args);
}
}
但是,当我尝试运行该应用时,它会抛出以下错误:
2017-03-27 13:55:58.281 INFO 8608 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-03-27 13:55:58.299 INFO 8608 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-03-27 13:55:58.302 INFO 8608 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-03-27 13:55:58.554 ERROR 8608 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase : A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122) [na:1.8.0_73]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) [na:1.8.0_73]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:939) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [tomcat-embed-core-8.5.11.jar:8.5.11]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_73]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_73]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_73]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_73]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.11.jar:8.5.11]
... 6 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [Pipeline[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5099) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.11.jar:8.5.11]
... 6 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.authenticator.NonLoginAuthenticator[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.core.StandardPipeline.startInternal(StandardPipeline.java:170) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.11.jar:8.5.11]
... 8 common frames omitted
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
at org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1137) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.11.jar:8.5.11]
... 10 common frames omitted
我当前的pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>XXXX</groupId>
<artifactId>XXXX</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- JSON -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.3</version>
</dependency >
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19.3</version>
</dependency>
<!-- REST Services -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.5.0.RELEASE</version>
</dependency> -->
<!-- SLF4J Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<!-- Set a compiler level -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
正如一些 SO 帖子中提到的,我尝试删除(Jersey),添加(嵌入式tomcat)依赖项。但这个错误似乎并没有对变化产生影响。
我还尝试过另一个来自Spring Tutorials的简单项目(app)(添加两个数字),这个有以下pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringBootDemoProject</groupId>
<artifactId>SpringBootDemoProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
这完全没问题。关于为什么会出现这种情况的任何指示?
答案 0 :(得分:3)
看起来你的类路径中有一个不兼容的Servlet API版本。 这可能是泽西岛罐子的依赖。 尝试从Spring启动器中拉出平针织机启动器。
org.springframework.boot : spring-boot-starter-jersey
这会将正确的版本带入您的类路径。
答案 1 :(得分:1)
我的猜测是你必须使用Eclipse ServletInitializer
中提供的默认Spring Boot Project template
类。
//SpringBootApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
//ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootApplication.class);
}
}
如果您未使用ServletInitializer
模板中指定的Spring Boot Starter Project
类,请尝试从App类扩展SpringBootServletInitializer
public class App extends SpringBootServletInitializer
请检查类似的答案here
答案 2 :(得分:0)
对我来说,将javax.servlet-api升级到4.0.1可以达到目的:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
答案 3 :(得分:0)
我在tomcat
中使用pom.xml
配置解决了这个问题
首先转到tomcat文件夹并在bin中打开cmd
,我的路径是:
C:\Program Files\Apache Software Foundation\Tomcat 8.5\bin>
使用此命令获取tomcat版本
C:\Program Files\Apache Software Foundation\Tomcat 8.5\bin>catalina.sh version
在此之后,将其添加到pom.xml
<properties>
<java.version>1.8</java.version>
<tomcat.version>8.5.43</tomcat.version>
</properties>
答案 4 :(得分:-1)
我有同样的问题,在评论下面后,它被修复了。其中一个依赖是罪魁祸首。
<!-- dependency>
<groupId>me.fhir</groupId>
<artifactId>fhir</artifactId>
<version>0.12.0-RELEASE</version>
</dependency-->
<!-- dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency-->
<!-- dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.8</version>
</dependency-->