缺少org.springframework.boot.web.servlet.server.ServletWebServerFactory

问题描述 投票:0回答:1

我正在运行程序并收到此错误 我是春天的新手 我用的是tomcat 8-5.8 我正在使用 eclipse maven 项目,并检查了简单项目

2023-09-22 15:11:02.621  INFO 12936 --- [           main] HelloWorldApp                            : Starting HelloWorldApp using Java 17.0.1 on KelvinT540 with PID 12936 (C:\tmp\myProject2\BatchTest\SpringConfig\target\classes started by Kelvin in C:\tmp\myProject2\BatchTest\SpringConfig)
2023-09-22 15:11:02.632  INFO 12936 --- [           main] HelloWorldApp                            : No active profile set, falling back to 1 default profile: "default"
2023-09-22 15:11:02.890  WARN 12936 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.context.MissingWebServerFactoryBeanException: No qualifying bean of type 'org.springframework.boot.web.servlet.server.ServletWebServerFactory' available: Unable to start AnnotationConfigServletWebServerApplicationContext due to missing ServletWebServerFactory bean
2023-09-22 15:11:02.936 ERROR 12936 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.

Action:

Check your application's dependencies for a supported servlet web server.
Check the configured web application type.

我试过这个:

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>org.netjs</groupId>
  <artifactId>SpringBootNetjs</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBootNetjs</name>
  <description>Spring Boot project</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
  </parent>
  <dependencies>
    <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>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

主文件:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;

@RestController
public class HelloController {
  @GetMapping(value="/{name}")
  public String displayMessage(@PathVariable("name") String name) {
    return "Hello " + name;
  }
}

运行它:

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

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

我应该改变什么吗?

PS。我问了 chatGPT、Google Bard、Lama,我不记得我还问了什么

spring spring-boot maven tomcat tomcat8
1个回答
0
投票

您需要将其更改为:

SpringApplication.run(HelloWorldApp.class, args);

此外,您不需要明确指定

@EnableAutoConfiguration
@SpringBootApplication
已经包含 3 个注释:
@Configuration
@EnableAutoConfiguration
@ComponentScan

请阅读以下内容: https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html

© www.soinside.com 2019 - 2024. All rights reserved.