I have a web service which handles GET/POST/PUT HTTP requests using a Spring Rest Controller (using Netty and not Apache Tomcat). I wish to filter all the requests coming in my service, and when a request has a certain header configured, I want to send this specific request to a whole different URL, while returning the response to the same entity which sent the original request.
Here is my code:
@Component
public class MyWebFilter implements WebFilter {
@Autowired
private SomeService someService;
private final Logger log = LoggerFactory.getLogger(MyWebFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
HttpHeaders headers = serverWebExchange.getRequest().getHeaders();
if (headers.containsKey("someHeader")) {
if (someService.askSomething(Objects.requireNonNull(headers.get("someHeader")))) {
URI originalUri = serverWebExchange.getRequest().getURI();
log.info("Redirecting request with URI {} to some service", originalUri.getPath());
try {
URI someUri = new URI("http",
originalUri.getUserInfo(),
someService.getHost(),
someService.getPort(),
originalUri.getPath(),
originalUri.getQuery(),
originalUri.getFragment());
ServerHttpRequest newRequest = serverWebExchange.getRequest().mutate().uri(someUri).build();
ServerWebExchange newExchange = serverWebExchange.mutate().request(newRequest).build();
return webFilterChain.filter(newExchange);
} catch (URISyntaxException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
return webFilterChain.filter(serverWebExchange);
}
}
With this implementation the request is simply passed through to my normal rest controller and does not reach the other service. What am I missing here?
Eventually I solved this by using Spring's WebClient (https://www.baeldung.com/spring-5-webclient) in order to resend the request to the external service.