spring - 应用程序运行失败的弹簧启动应用程序
问题描述
我是春季靴子开发的新手。我创建了我的第一个 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>
解决方案
这是您正在创建的 Web 应用程序吗?
如果没有,请尝试在 application.yml 文件中设置此属性:
spring:
main:
web-application-type: none
如果是这样,您需要有一个ServletWebServerFactory bean。一个例子:
MyConfiguration {
@Bean
ServletWebServerFactory servletWebServerFactory(){
return new TomcatServletWebServerFactory();
}
}
更多信息在这里。
哦,安装Maven。
推荐阅读
- react-native - React Native Webview - 与 Expo - 不显示来自 s3 存储桶的图像
- sql-server - 在 T-SQL 中使用 Top
- php - 更新数组中的表值
- ruby - 使用 selenium chromedriver 创建登录到我的帐户的会话时出现错误
- php - 如何创建 php 表单并集成链接到 Telegram 或 Whatsapp 的推送通知系统?
- cordova - 错误:标头值必须是字符串错误:高级http:标头值必须是字符串离子
- javascript - 使用 GitLab 持续部署 NodeJS
- r - R shinydashboard:各种菜单子项的动态和静态tabItems的混合
- linux - 为什么 shell 脚本中的“读取”命令缺少初始字符?
- laravel - 工作职能只是保持处理而不被处理