由于缺少 ServletWebServerFactory,无法启动 Web 服务器

Posted

技术标签:

【中文标题】由于缺少 ServletWebServerFactory,无法启动 Web 服务器【英文标题】:Unable to start web server due to missing ServletWebServerFactory 【发布时间】:2019-12-01 15:29:50 【问题描述】:

几天前我看过 Stack Overflow。我的问题似乎很常见,但我无法解决它。

我已经尝试过1 和2 的这些方法。

这是我的 application.propreties :

spring.datasource.driver-class-name= org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5433/mydatabase
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
spring.main.web-application-type=none

当我尝试运行它时,它给了我以下错误:

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

这是我的主要课程:

@SpringBootApplication
public class StartApplication 

    @Autowired
    BookRepository repository;

    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>toto</groupId>
    <artifactId>toto</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <downloadSources>true</downloadSources>
        <downloadJavadocs>true</downloadJavadocs>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>
</project>

提前感谢您的帮助。

【问题讨论】:

在idea或者eclipse debug模式下运行spring boot,你就知道问题所在了。 你能展示你的主要课程吗? 你能给我们你的 pom.xml 或 build.gradle 吗? 感谢我在帖子中添加了 pom 和主类, 【参考方案1】:

您应该从 application.properties 文件中删除以下属性

spring.main.web-application-type=none

这意味着您的应用程序中不需要网络服务器。

【讨论】:

好吧,我做了,但出现了一个新错误:无法在项目 toto 上执行目标 org.springframework.boot:spring-boot-maven-plugin:2.1.2.RELEASE:run (default-cli) : 运行时发生异常。 null:InvocationTargetException:无法启动 Web 服务器;嵌套异常是 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean。 -> [帮助 1]【参考方案2】:

我不是专业程序员,但如果我是你我会怎么做:

    确保您有合适的项目结构者 查看Spring.io的基本配置指南!

我有我的个人项目,我在其中使用 Spring Framework 进行培训,也许你会从中得到一些有用的东西,project link!

祝你好运,玩得开心:)

【讨论】:

感谢 thomas,但我尝试遵循 baeldung 的 tuto (baeldung.com/spring-boot-hibernate)。它不起作用。 尝试比较我的配置文件和 pom.xml 和 yoursm 也许它会帮助你【参考方案3】:

你的主类应该是这样的:

@SpringBootApplication
public class StartApplication 
    
    @Autowired
    BookRepository repository;
    
    public static void main(String[] args) 
        SpringApplication.run(StartApplication.class, args);
    

【讨论】:

以上是关于由于缺少 ServletWebServerFactory,无法启动 Web 服务器的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 由于缺少调试目标

由于缺少位置权限,应用程序在启动时崩溃

由于缺少对象,DACPAC 无法部署

由于缺少 KIF 类,项目无法构建

“场景由于缺少入口点而无法访问并且没有...” - 解决此警告

由于缺少文件,快照无法使用