Jin Kwon
Jin Kwon

Reputation: 21992

How can I forward request using HandlerFilterFunction?

A server environment requires an endpoint for /some/health.

I already configured actuator.

Rather changing the actuator's function, I'm thinking forwarding /some/health to the /actuator/health.

And I'm trying to do with HandlerFilterFunction.

@Configuration
public class SomeHealthFilterFunction
        implements HandlerFilterFunction<ServerResponse, ServerResponse> {

    private static final String PATTERN = "/some/health";

    @Override
    public Mono<ServerResponse> filter(ServerRequest request,
                                       HandlerFunction<ServerResponse> next) {
        if (PATTERN.equals(request.requestPath().pathWithinApplication().value())) {
            RequestPath requestPath
                    = request.requestPath().modifyContextPath("/actuator/health");
            // How can I call next.handle, here?
        }
    }
}

How can I change the origin request and do next.handle(...)?

Upvotes: 0

Views: 772

Answers (1)

Michael McFadyen
Michael McFadyen

Reputation: 2995

Here's an example WebFilter that will reroute all calls from /some/health to /actuator/health

@Component
public class RerouteWebFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
        ServerHttpRequest request = serverWebExchange.getRequest();
        if ("/some/health".equals(request.getPath().pathWithinApplication().value())) {
            ServerHttpRequest mutatedServerRequest = request.mutate().path("/actuator/health").build();
            serverWebExchange = serverWebExchange.mutate().request(mutatedServerRequest).build();
        }

        return webFilterChain.filter(serverWebExchange);
    }
}

Upvotes: 1

Related Questions