相关疑难解决方法(0)

关于spring boot如何正确禁用web环境

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)

这次它有效.

为什么前两种方式不起作用?

spring-boot

9
推荐指数
2
解决办法
2万
查看次数

Spring Boot - NoClassDefFoundError: javax/servlet/Filter && 尝试使用 Netty 时无法启动 ServletWebServerApplicationContext

我正在尝试使用 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。

我已经检查了几个问题,但还没有运气。

有任何想法吗?

环境

  • 引导:2.1.3.RELEASE
  • 日食:4.7

java netty spring-boot spring-webflux

3
推荐指数
1
解决办法
5821
查看次数

Spring WebClient - 在发生 HTTP 错误(4xx、5xx)时如何访问响应正文?

我想将异常从“数据库”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)

正如您所看到的,原始的“消息”字段丢失了。

我用:

  • 春季启动 2.2.5.RELEASE
  • spring-boot-启动器-web
  • spring-boot-starter-webflux

后端和数据库从 Tomcat 开始(web 和 webflux 在同一应用程序中)。

这是后端:

    @GetMapping(path = "/test/{id}")
    public Mono<Integer> test(@PathVariable String id) {
        return …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-webflux spring-webclient

3
推荐指数
2
解决办法
2万
查看次数

spring-boot-starter-web 和 spring-boot-starter-webflux 的区别?

我当时尝试包含上述依赖项之一,但找不到任何区别:

  • spring boot starter web:我可以看到 Flux 和 Mono 类并制作一个反应式休息控制器
  • spring boot starter webflux:我可以看到rest控制器类和注释,我可以制作一个规范的rest控制器

那么,我错过了什么?使用弹簧靴 2.2.5-RELEASE。

spring-boot

3
推荐指数
1
解决办法
1980
查看次数