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
A non-blocking way would be via one of the overloaded subscribe() methods. In this example, we will use the subscribe(Consumer<? super T> consumer) to get the data from Mono asynchronously. With subscribe(), the current thread will not be blocked waiting for the Publisher to emit data.
Method SummaryBuild a ServerWebExchange decorator with the mutated properties. Set the Mono<Principal> to return for this exchange. Configure a consumer to modify the current request using a builder. Set the request to use especially when there is a need to override ServerHttpRequest methods.
public interface ServerWebExchange. Contract for an HTTP request-response interaction. Provides access to the HTTP request and response and also exposes additional server-side processing related properties and features such as request attributes.
Use Spring MVC with WebFlux to build Java-based web applications. Employ the various Spring MVC architectures. Work with controllers and routing functions. Build microservices and web services using Spring MVC and REST.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With