无法启动web服务器;由于缺少servletwebserverfactory bean,嵌套异常无法启动servletwebserverapplicationcontext

goqiplq2  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(281)

相关项目在这里(实际代码来自fixthisspring分支):https://github.com/sekassel/coronatrackeresp32/tree/master/server
类似的问题,但没有为我解决:applicationcontextexception:由于缺少ServletWebServerFactoryBean,无法启动servletwebserverapplicationcontext
概括地说,主要问题是:

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

我用springboot构建了一个小的vaadin应用程序,它在本地运行。如果我尝试构建dockerfile并运行它,它会崩溃:

19.2.2021 13:14:30 __  __          ____                  _              _
19.2.2021 13:14:30|  \/  | _   _  |  _ \  _ __   ___    (_)  ___   ___ | |_
19.2.2021 13:14:30| |\/| || | | | | |_) || '__| / _ \   | | / _ \ / __|| __|
19.2.2021 13:14:30| |  | || |_| | |  __/ | |   | (_) |  | ||  __/| (__ | |_
19.2.2021 13:14:30|_|  |_| \__, | |_|    |_|    \___/  _/ | \___| \___| \__|
19.2.2021 13:14:30         |___/                      |__/
19.2.2021 13:14:30
19.2.2021 13:14:30[pool-2-thread-1] INFO org.springframework.boot.SpringApplication - Starting application using Java 12-ea on accda21c508b with PID 1 (started by root in /server)
19.2.2021 13:14:30[pool-2-thread-1] INFO org.springframework.boot.SpringApplication - No active profile set, falling back to default profiles: default
19.2.2021 13:14:30[pool-2-thread-1] WARN org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: 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.
19.2.2021 13:14:30[pool-2-thread-1] ERROR org.springframework.boot.SpringApplication - Application run failed
19.2.2021 13:14:30org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

我猜是因为依赖关系?这里是build.gradle:

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'com.vaadin' version '0.14.3.7'
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

group 'projekt'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

ext {
    set('vaadinVersion', "14.4.6")
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile "com.sparkjava:spark-core:2.8.0"
    compile 'org.slf4j:slf4j-simple:1.7.21'
    compile 'org.json:json:20200518'
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'
    compile 'com.fasterxml.jackson.core:jackson-core:2.11.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
    compile 'com.google.protobuf:protobuf-java:3.12.2'
    compile 'at.favre.lib:hkdf:1.1.0'
    compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.32.3.2'

    // Vaadin
    implementation 'com.vaadin:vaadin-spring-boot-starter'
    implementation 'com.vaadin:vaadin-avatar-flow:1.0.1'
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
    }
}

dependencyManagement {
    imports {
        mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
    }
}

test {
    useJUnitPlatform()
}

bootJar {
    mainClassName = 'Main'
}

jar {
    manifest {
        attributes 'Main-Class': 'Main'
    }
}

和dockerfile:

FROM gradle:5.6.4-jdk11 as builder
COPY --chown=gradle:gradle . /server
WORKDIR /server
RUN gradle shadowJar

FROM openjdk:8-jdk-alpine
WORKDIR /server
COPY --from=builder /server/build/libs .
EXPOSE 4567 8080
CMD ["java", "-jar", "server-1.0-SNAPSHOT-all.jar"]

更新:我将dockerfile更改为: RUN gradle bootJar 现在spring启动了,但是vaadin似乎有一个问题要找

[pool-2-thread-1] INFO NodeInstaller - Extracting NPM
[pool-2-thread-1] INFO NodeInstaller - Local node installation successful.
[pool-2-thread-1] ERROR dev-updater - Error when running `npm install`
java.io.IOException: Cannot run program "/root/.vaadin/node/node" (in directory "/server"): error=2, No such file or directory
7cwmlq89

7cwmlq891#

首先必须运行bootjar而不是shadowjar。第二,你必须建立在生产模式

RUN gradle bootJar -Pvaadin.productionMode

请阅读vaadin文档中有关生产模式的更多信息https://vaadin.com/docs/flow/production/tutorial-production-mode-basic.html

相关问题