首页 > 解决方案 > 在 RestController 方法中获取 ServerWebExchange

问题描述

如何在控制器方法中获取ServerWebExchange或响应?ServerHttpResponse

NestedServletException在尝试调用接受的休息控制器方法时不断得到ServerWebExchange

我的控制器如下所示:

@RestController
@RequestMapping(path = "/path/{param1}/path", produces = MediaType.APPLICATION_JSON_VALUE)
public class MyController {
    @GetMapping("/path")
    public Mono<Void> method(ServerWebExchange exchange) {
        ...
    }
}

主要方法:

@SpringBootApplication
@ComponentScan(basePackages = {"my.package1", "my.package2"})
@EnableWebFlux
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

例外:

io.undertow.request : UT005023: 对 /path/value/path/path 的异常处理请求

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.web.server.ServerWebExchange

标签: javaspringspring-bootspring-webflux

解决方案


解决方案:确保您spring-webmvc在类路径上没有。

我正在迁移到spring-webflux,但得到spring-webmvc了传递依赖。webflux并且webmvc不兼容 - 只有一个会处理请求。


推荐阅读