简体   繁体   中英

Getting ServerWebExchange in RestController method

How can I get ServerWebExchange or reactive ServerHttpResponse in controller method?

I'm constantly getting NestedServletException while trying to call rest controller method that accepts ServerWebExchange .

My controller looks like this:

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

Main method:

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

Exception:

io.undertow.request : UT005023: Exception handling request to /path/value/path/path

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.web.server.ServerWebExchange

Solution : Make sure you have no spring-webmvc on classpath.

I was migrating to spring-webflux , but got spring-webmvc as a transitive dependency. webflux and webmvc are incompatible - only one would handle requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM