Spring Webflux throws different exception when called from Web and WebTestClient

170 Views Asked by At

I have a controller which has a GET mapping with a couple of request params and a request header. Something like this

@GetMapping
public Mono<Response> getAll(@RequestParam("someId") String someId,
                             @RequestParam("someOtherId") String someOtherId,
                             @RequestHeader(SOME_TOKEN) String someToken) { }

I have a controller advice where I am catching exceptions and sending customized messages and http codes back. I noticed that when I call from web(or via some rest client), if any of the request params or header is missing, following exceptions are thrown,

org.springframework.web.bind.MissingServletRequestParameterException
org.springframework.web.bind.MissingRequestHeaderException

In my unit tests I am using

@WebFluxTest(controllers = MyController.class)

and

@Autowired
private WebTestClient webClient;

When I try calling the endpoint without required param or header, I get the folowwing exception

org.springframework.web.server.MissingRequestValueException

Because of this my @ExceptionHandlers in Controller advices are not triggered.
Is this a mistake or am I missing something here.

Shouldn't spring throw the same exception irrespective of where the call is coming from? If that is not the case, we cannot really test controller advices.

0

There are 0 best solutions below