【问题标题】:Spring Boot working only in the IDESpring Boot 仅在 IDE 中工作
【发布时间】:2018-07-10 21:14:21
【问题描述】:

我正在使用 IntelliJ 开发一个 Spring Boot 应用程序,并且我正在尝试创建一个可执行的 JAR。

在 IDE 中一切正常,但是当我创建一个可执行的 JAR 并通过 CMD 运行它时,我得到了各种错误。

我的 Main 看起来像这样:

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

然后我得到了这个:

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
    at my.app.Main.main(Main.java:9)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)
    ... 8 common frames omitted

我找不到为什么会这样以及如何解决它。任何帮助将不胜感激。

我的 Gradle 构建文件:

plugins {
    id 'java'
}

group 'my.app'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
    flatDir {
        dirs 'lib'
    }
}

dependencies {
    compile 'com.graphql-java:graphql-java:9.0'
    compile 'com.graphql-java:graphql-java-tools:5.1.0'
    compile 'com.graphql-java:graphql-spring-boot-starter:4.2.0'
    compile 'org.springframework.data:spring-data-couchbase:3.0.8.RELEASE'
    compile 'org.hibernate:hibernate-core:5.3.2.Final'
    compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2'
    compile 'com.beust:klaxon:3.0.1'
    compile fileTree(dir: 'lib', includes: ['*.jar'])
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

lib 目录包含以下文件:

classmate-1.3.4.jar
hibernate-validator-6.0.9.Final.jar
jackson-annotations-2.9.0.jar
jackson-core-2.9.5.jar
jackson-databind-2.9.5.jar
jackson-datatype-jdk8-2.9.5.jar
jackson-datatype-jsr310-2.9.5.jar
jackson-module-parameter-names-2.9.5.jar
javax.annotation-api-1.3.2.jar
jboss-logging-3.3.2.Final.jar
jul-to-slf4j-1.7.25.jar
log4j-api-2.10.0.jar
log4j-to-slf4j-2.10.0.jar
logback-classic-1.2.3.jar
logback-core-1.2.3.jar
reactive-streams-1.0.2.jar
slf4j-api-1.7.25.jar
snakeyaml-1.19.jar
spring-aop-5.0.5.RELEASE.jar
spring-aop-5.0.6.RELEASE.jar
spring-aspects-5.0.5.RELEASE.jar
spring-beans-5.0.5.RELEASE.jar
spring-beans-5.0.6.RELEASE.jar
spring-boot-2.0.2.RELEASE.jar
spring-boot-autoconfigure-2.0.2.RELEASE.jar
spring-boot-starter-2.0.2.RELEASE.jar
spring-boot-starter-json-2.0.2.RELEASE.jar
spring-boot-starter-logging-2.0.2.RELEASE.jar
spring-boot-starter-tomcat-2.0.2.RELEASE.jar
spring-boot-starter-web-2.0.2.RELEASE.jar
spring-context-5.0.5.RELEASE.jar
spring-context-5.0.6.RELEASE.jar
spring-context-indexer-5.0.5.RELEASE.jar
spring-context-support-5.0.5.RELEASE.jar
spring-core-5.0.5.RELEASE.jar
spring-core-5.0.6.RELEASE.jar
spring-expression-5.0.5.RELEASE.jar
spring-expression-5.0.6.RELEASE.jar
spring-instrument-5.0.5.RELEASE.jar
spring-jcl-5.0.5.RELEASE.jar
spring-jcl-5.0.6.RELEASE.jar
spring-jdbc-5.0.5.RELEASE.jar
spring-jms-5.0.5.RELEASE.jar
spring-messaging-5.0.5.RELEASE.jar
spring-orm-5.0.5.RELEASE.jar
spring-oxm-5.0.5.RELEASE.jar
spring-test-5.0.5.RELEASE.jar
spring-tx-5.0.5.RELEASE.jar
spring-web-5.0.5.RELEASE.jar
spring-web-5.0.6.RELEASE.jar
spring-webflux-5.0.5.RELEASE.jar
spring-webmvc-5.0.5.RELEASE.jar
spring-webmvc-5.0.6.RELEASE.jar
spring-websocket-5.0.5.RELEASE.jar
sqlite-jdbc-3.21.0.jar
Hibernate.jar
tomcat-embed-core-8.5.31.jar
tomcat-embed-el-8.5.31.jar
tomcat-embed-websocket-8.5.31.jar
validation-api-2.0.1.Final.jar

【问题讨论】:

  • 您需要添加TomcatServletWebServerFactory 的事实已经表明有问题。发布您的 pom.xml 或 build.gradle 并显示依赖关系,并发布完整的堆栈跟踪,而不仅仅是消息。
  • 最重要的信息是异常的原因,它将在堆栈跟踪中一路向下。请张贴。
  • @M.Deinum 我根据您的评论编辑了问题
  • 在你的构建文件中没有任何与启动相关的东西......那么spring boot应该如何知道如何构建一个可执行的jar并包含正确的依赖项?它们不在您的依赖项列表中。
  • @M.Deinum 我的 lib 文件夹中有 Spring Boot JAR 文件。 IntelliJ 工件设置是将 lib 文件夹中的所有 JAR 提取到最终的可执行 JAR 中。

标签: java spring spring-boot tomcat


【解决方案1】:

尝试在你的 pom.xml 中使用这个:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

或者/还有这个:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

并尝试删除:

 @Bean
public ServletWebServerFactory servletWebServerFactory() {
    return new TomcatServletWebServerFactory();
}

从主要

【讨论】:

  • 我有这两个jar文件
  • 奇怪.. 但是通常我在生成 jar 文件时不会在主文件中插入此代码:@Bean public ServletWebServerFactory servletWebServerFactory() { return new TomcatServletWebServerFactory(); }
猜你喜欢
  • 1970-01-01
  • 2019-01-30
  • 2016-12-08
  • 2021-06-19
  • 1970-01-01
  • 2021-05-21
  • 2019-08-15
  • 1970-01-01
  • 2019-06-06
相关资源
最近更新 更多