Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 522
senia Avatar asked Jul 15 '18 14:07

senia


People also ask

How do you get an object from mono without blocking?

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.

How do I make ServerWebExchange?

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.

What is ServerWebExchange?

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.

Can I use Springmvc and WebFlux together?

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.


1 Answers

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.

like image 165
senia Avatar answered Sep 28 '22 17:09

senia