Spring Web转Spring Reactive Web后Webflux控制器空响应问题求助
将Spring Web应用改造为Spring Reactive Web后,新增了返回Mono/Flux类型结果的控制器,但通过Postman调用接口时,HTTP响应码为200却没有响应体。
控制台日志如下:
022-08-23 15:00:11.315 [http-nio-8083-exec-5] [user=kashif]
[request_id=8df3ef65-e9b7-4aaf-b3ef-65e9b7eaaf31]
[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]
DEBUG - Mapped to
org.dummy.services.controller.TestController#hello()
2022-08-23 15:00:11.315 [http-nio-8083-exec-5] [user=kashif]
[request_id=8df3ef65-e9b7-4aaf-b3ef-65e9b7eaaf31]
[org.dummy.services.controller.TestController] INFO - POI hello
2022-08-23 15:00:11.315 [http-nio-8083-exec-5] [user=qaz] [request_id=8aaaaaaaaaaaaaf31]
[org.springframework.web.context.request.async.WebAsyncManager] DEBUG - Started async request
2022-08-23 15:00:11.315 [http-nio-8083-exec-5] [user=qaz]
[request_id=8aaaaaaaaaaaaaf31]
[org.springframework.web.context.request.async.WebAsyncManager] DEBUG - Async result set, dispatch to /poi/test/hello
2022-08-23 15:00:11.315 [http-nio-8083-exec-5] [user=qaz]
[request_id=8df3ef65-e9b7-4aaf-b3ef-65e9b7eaaf31]
[org.springframework.web.servlet.DispatcherServlet] DEBUG - Exiting but response remains open for further handling
2022-08-23 15:00:11.346 [http-nio-8083-exec-5] [user=] [request_id=]
[org.springframework.web.servlet.DispatcherServlet] DEBUG - "ASYNC" dispatch for POST "/poi/test/hello", parameters={masked}
2022-08-23 15:00:11.346 [http-nio-8083-exec-5] [user=] [request_id=]
[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]
DEBUG - Resume with async result ["hello"]
2022-08-23 15:00:11.346 [http-nio-8083-exec-5] [user=] [request_id=]
[org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor]
DEBUG - Using 'text/plain', given [/] and supported [text/plain, /, text/plain, /, application/json, application/+json, application/json, application/+json, application/cbor]
2022-08-23 15:00:11.346 [http-nio-8083-exec-5] [user=] [request_id=]
[org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor]
DEBUG - Writing ["hello"]
2022-08-23 15:00:11.346 [http-nio-8083-exec-5] [user=] [request_id=]
[org.springframework.web.servlet.DispatcherServlet] DEBUG - Exiting from "ASYNC" dispatch, status 200
示例控制器代码:
@RestController @RequestMapping("test") public class TestController { @RequestMapping(value = "/hello", method = RequestMethod.POST) public Mono<String> hello() { log.info("POI hello"); return Mono.just("hello");//.map(ResponseEntity::ok); } }
相关版本信息:
- Spring Boot版本:2.2.5
- Java版本:8
从日志可以看出,应用仍在使用org.springframework.web.servlet相关组件(传统Servlet栈),而非WebFlux的Reactive栈。这意味着改造时没有完全切换到WebFlux环境,导致Mono类型被当作普通异步Servlet请求处理,响应体未能正确返回。
1. 切换依赖到WebFlux
移除传统Spring Web依赖,添加Spring WebFlux依赖:
Maven配置
<!-- 移除原有Spring Web依赖 --> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> --> <!-- 添加WebFlux依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
Gradle配置
// 移除原有Spring Web依赖 // implementation 'org.springframework.boot:spring-boot-starter-web' // 添加WebFlux依赖 implementation 'org.springframework.boot:spring-boot-starter-webflux'
2. 优化控制器注解(可选)
使用WebFlux推荐的简化注解,替代@RequestMapping:
@RestController @RequestMapping("test") public class TestController { @PostMapping("/hello") public Mono<String> hello() { log.info("POI hello"); return Mono.just("hello"); } }
3. 清理Servlet相关配置
确保应用启动类没有添加@ServletComponentScan等Servlet栈专属注解,避免同时启动两种环境。
4. 检查Postman请求设置
- 确认请求方法为POST,路径与控制器配置一致
- 确保Headers中包含
Accept: text/plain或Accept: */*(日志显示已支持该类型) - 关闭"Send and Download"模式,使用普通Send模式发送请求
内容的提问来源于stack exchange,提问作者oOXAam

