Dexter Dexter - 19 days ago 6
Java Question

Reactive Spring does not support HttpServletRequest as parameter in REST endpoint?

I created a RestController which look like this :

@RestController
public class GreetingController {

@RequestMapping(value = "/greetings", method = RequestMethod.GET)
public Mono<Greeting> greeting(HttpServletRequest request) {

return Mono.just(new Greeting("Hello..." + request.toString()));
}
}


Unfortunately when I try to hit the "greetings" endpoint I get an exception :


java.lang.IllegalStateException: No resolver for argument [0] of type
[org.apache.catalina.servlet4preview.http.HttpServletRequest]


I am using


compile('org.springframework.boot.experimental:spring-boot-starter-web-reactive')



How to fix this ?

Link to full stack-trace. Link to build.gradle

----------EDIT----------

Using the interface. Now getting :


java.lang.IllegalStateException: No resolver for argument [0] of type
[javax.servlet.http.HttpServletRequest] on method (rest is same)

Answer

You should never use the Servlet API in a Spring Reactive Web application. This is not supported and this is making your app container-dependent, whereas Spring Web Reactive can work with non-Servlet runtimes such as Netty.

Instead you should use the HTTP API provided by Spring; here's your code sample with a few changes:

@RestController
public class GreetingController {

    @GetMapping("/greetings")
    public Mono<Greeting> greeting(ServerHttpRequest request) {

        return Mono.just(new Greeting("Hello..." + request.getURI().toString()));
    }
}

You can inject either ServerWebExchange or directly ServerHttpRequest / ServerHttpResponse.

Comments