1

I am building a Spring Boot API. Its in prod, and we just learned that incoming requests will include forward slashes in the path variables. I tested this and it breaks (getting a 404 path not found error). My client has no bandwidth to handle a contract change, so Im trying to do the civil thing here and handle this on my end. So customerId here may include between 0 and whatever number of forward slashes.

@EnableWebFlux
@Configuration
public class PurchaseRouter {

    private static final String ITEM_PATH = "/store/{storeId}/customers/{customerId}/item/{itemId}/"

    @Bean
    public RouterFunction<ServerResponse> routes(SomeHandler someHandler) {
        return route(RequestPredicates.PUT(ITEM_PATH), (ServerRequest serverRequest) -> someHandler.handleRequest(serverRequest));

    }
}

I've seen other answers here that handle this problem with controllers. This is a quick fix that may work for paths with only 1 parameter. In my case, I'm using the new Functional Router and I have 3 parameters, and thus that approach doesn't really work for me. I also don't want to handle this problem via regex because its smelly. I suspect Netty and Spring Boot, when configured correctly, could handle this use case.

How do I configure Spring Boot and Netty to allow forward slashes in the path parameters? I tried implementing the interface WebServerFactoryCustomizer but didn't get far.

@Configuration
public class NettyConfig implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory serverFactory) {
        NettyServerCustomizer nettyServerCustomizer = new NettyServerCustomizer() {
            @Override
            public HttpServer apply(HttpServer httpServer) {
                //insert magic here
            }
        }
    }
}
user197452
  • 25
  • 4
  • i'm no expert in this field, but the first argument to the `route()` function takes a predicate that returns a boolean to match on routes. Can't you write your own parsing function that returns true if the path has a forward slash or not? – Toerktumlare Sep 04 '19 at 12:20
  • I rather not write any parsing functions. I feel like this should be a feature supported by Spring Boot and Netty. – user197452 Sep 04 '19 at 14:37
  • when choosing to work with router functions in webflux you make a choice to work as low level as possible where nothing is given to you for free. Then my guess is that no support for this is given. If you dont "feel like" writing your own you can use annotated controllers https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-controller If you "feel" this should be a feature, supported, well then it actually is, in the annotated use cases. The functional approch is for them that want total control. – Toerktumlare Sep 04 '19 at 18:07
  • If you still stand by your opinion, then you are free to open a feature request and see if it gets accepted https://github.com/spring-projects/spring-framework/issues – Toerktumlare Sep 04 '19 at 18:10

0 Answers0