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
}
}
}
}