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)
The code to get it is as follows. ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder. getRequestAttributes(); // get the request HttpServletRequest request = requestAttributes. getRequest();
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder. getRequestAttributes()) . getRequest();
Spring Boot HttpServletRequest Logging Filter. A Request Logging Filter for Spring Boot applications, that allows you to read the request body multiple times. The request body is cached using an overridden HttpServletRequestWrapper. Then a reader that allows the client to read the body multiple times is returned.
Pass it to the constructor: public class XmlParser{ final private HttpServletRequest request; public XmlParser(HttpServletRequest request) { this. request = request; } // use it in othe methods... }
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:
import org.springframework.http.server.reactive.ServletServerHttpRequest; @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
.
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