gpt4 book ai didi

java - 我正在研究以下 Spring Boot 示例,并且收到 ApplicationContextException

转载 作者:行者123 更新时间:2023-12-02 01:00:26 25 4
gpt4 key购买 nike

我使用了不同版本的 tomcat,但由于缺少 ServletWebServerFactory bean,我收到错误,无法启动 ServletWebServerApplicationContext。我在启动时收到上述错误,有什么快速帮助吗?

错误

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)

2020-03-16 23:04:01.268 INFO 3452 --- [ main] c.example.demo.CoursesWebappApplication : Starting CoursesWebappApplication on DESKTOP-10R8SDT with PID 3452 (D:\workspace-spring-tool-suite-4-4.5.1.RELEASE\CoursesWebapp\target\classes started by admin in D:\workspace-spring-tool-suite-4-4.5.1.RELEASE\CoursesWebapp)
2020-03-16 23:04:01.268 INFO 3452 --- [ main] c.example.demo.CoursesWebappApplication : No active profile set, falling back to default profiles: default
2020-03-16 23:04:01.581 WARN 3452 --- [ main] ConfigServletWebServerApplicationContext : 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.
2020-03-16 23:04:01.596 ERROR 3452 --- [ main] o.s.boot.SpringApplication : Application run failed

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:156) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]

Controller 类

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;



@Controller
public class CoursesController {
@RequestMapping(value="/course", method = RequestMethod.GET)
public void courses () {
System.out.println("welcome");
}
}

演示类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;

@SpringBootConfiguration
public class CoursesWebappApplication {

public static void main(String[] args) {
SpringApplication.run(CoursesWebappApplication.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>CoursesWebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CoursesWebapp</name>
<description>Demo project for Spring Boot</description>

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

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

<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>

</project>

最佳答案

我想您正在使用 Spring boot,因此请使用 @SpringBootApplication 而不是 @SpringBootConfiguration 以确保 Spring 为您的项目提供所有需要的自动配置。

关于java - 我正在研究以下 Spring Boot 示例,并且收到 ApplicationContextException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com