Spring启动非Web应用程序,启动时有以下错误
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
Run Code Online (Sandbox Code Playgroud)
然后我尝试了下面的方式
new SpringApplication().setWebEnvironment(false);
Run Code Online (Sandbox Code Playgroud)
然后开始它仍然有上面的错误.
然后试过
@SpringBootApplication(exclude={SpringDataWebAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)
但仍然有同样的错误.
最后我尝试添加以下配置 application.properties
spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)
这次它有效.
为什么前两种方式不起作用?
我正在尝试使用 Netty 服务器。所以我在pom.ml文件中排除了Tomcat;
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我收到了由以下原因引起的错误:java.lang.NoClassDefFoundError: javax/servlet/Filter
然后我添加依赖项
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后再次运行应用程序时,我收到错误 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext 由于缺少 ServletWebServerFactory bean。
我已经检查了几个问题,但还没有运气。
有任何想法吗?
我想将异常从“数据库”REST API 重新抛出到“后端”REST API,但我丢失了原始异常的消息。
这是我通过 Postman 从“数据库”REST API 获得的信息:
{
"timestamp": "2020-03-18T15:19:14.273+0000",
"status": 400,
"error": "Bad Request",
"message": "I'm DatabaseException (0)",
"path": "/database/api/vehicle/test/0"
}
Run Code Online (Sandbox Code Playgroud)
这部分还可以。
这是我通过 Postman 从“后端”REST API 获得的信息:
{
"timestamp": "2020-03-18T15:22:12.801+0000",
"status": 400,
"error": "Bad Request",
"message": "400 BAD_REQUEST \"\"; nested exception is org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from GET http://localhost:8090/database/api/vehicle/test/0",
"path": "/backend/api/vehicle/test/0"
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,原始的“消息”字段丢失了。
我用:
后端和数据库从 Tomcat 开始(web 和 webflux 在同一应用程序中)。
这是后端:
@GetMapping(path = "/test/{id}")
public Mono<Integer> test(@PathVariable String id) {
return …Run Code Online (Sandbox Code Playgroud) 我当时尝试包含上述依赖项之一,但找不到任何区别:
那么,我错过了什么?使用弹簧靴 2.2.5-RELEASE。