【问题标题】:Application run failed spring boot aplication应用程序运行失败 Spring Boot 应用程序
【发布时间】:2020-10-26 17:22:42
【问题描述】:

我是 Spring Boot 开发的新手。我创建了我的第一个 Spring Boot 应用程序,当我尝试将它作为 Spring Boot 应用程序运行时,我收到了这条消息。 无法启动网络服务器;嵌套异常是 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {

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

}

我的配置类:

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppConfiguration {
@RequestMapping("/hello")
    public String hellow() {
        return "hellow world";
    }

}

我的 pom.xml 看起来像:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorld</name>
    <description>Hello World Project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

【问题讨论】:

  • spring-boot-starter-tomcat 已包含在 spring-boot-starter-web 中。删除它并重试。另外,你有配置类吗?
  • 当我删除 spring-boot-starter-tomcat 时它不起作用
  • 是的,我有配置类``` package com.example.demo;导入 org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.bind.annotation.RestController; @RestController public class AppConfiguration { @RequestMapping("/hello") public String hello() { return "hellow world"; } ```
  • 请发布整个日志。
  • 如何启动这个应用程序?

标签: spring spring-boot


【解决方案1】:

这是您正在创建的 Web 应用程序吗?

如果没有,请尝试在 application.yml 文件中设置此属性:

spring:
  main:
    web-application-type: none

如果是这样,您需要有一个ServletWebServerFactory bean。一个例子:

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

更多信息here。

哦,安装 maven。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2019-04-01
    • 2020-05-14
    • 2017-05-12
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多