问题
We are running WireMock on Spring-boot with its embedded Tomcat 9, I am able to stub the request using WM if the stub request/response are available in mappings/__files directory respectively. The issue is when trying to query the list of available stubs using http://localhost:8080/__admin/mappings
we get the below error.
ERROR 19332 --- [ultThreadPool-1] WireMock :
Request was not matched
=======================
-----------------------------------------------------------------------------------------------------------------------
| Closest stub | Request |
-----------------------------------------------------------------------------------------------------------------------
|
GET | GET
/some/thing | /__admin/mappings <<<<< URL does not match
|
|
-----------------------------------------------------------------------------------------------------------------------
My starter class looks like this:
@SpringBootApplication (exclude = {DispatcherServletAutoConfiguration.class})
public class Starter extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureApp(builder);
}
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
private static SpringApplicationBuilder configureApp(SpringApplicationBuilder builder) {
return builder.sources(Starter.class);
}
}
WireMockConfig by extending ResourceConfig class as below
@Configuration
@ComponentScan(basePackages = {"com.github.tomakehurst.*"})
public class WireMockResourceConfig extends ResourceConfig {
@Bean
public WireMockHandlerDispatchingServlet dispatcherServlet() {
return new WireMockHandlerDispatchingServlet();
}
@Bean
public ServletListenerRegistrationBean<ServletContextListener> listenerRegistrationBean() {
ServletListenerRegistrationBean<ServletContextListener> bean = new ServletListenerRegistrationBean<>();
bean.setListener(new WireMockWebContextListener());
return bean;
}
@Bean
public ServletContextInitializer fileSourceRoot() {
return (ServletContext servletContext) -> {
servletContext.setInitParameter("WireMockFileSourceRoot",
"/WEB-INF/wiremock");
};
}
@Bean
public ServletRegistrationBean<Servlet> dispatcherAdminServletRegistrationBean() {
ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>(dispatcherServlet());
Map<String, String> params = new HashMap<String, String>();
params.put("RequestHandlerClass", StubRequestHandler.class.getName());
registration.setInitParameters(params);
List<String> urlMappings = Arrays.asList("/*");
registration.setUrlMappings(urlMappings);
return registration;
}
@Bean
public ServletRegistrationBean<Servlet> dispatcherServletRegistrationBean() {
ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>(dispatcherServlet());
Map<String, String> params = new HashMap<String, String>();
params.put("RequestHandlerClass", AdminRequestHandler.class.getName());
registration.setInitParameters(params);
List<String> urlMappings = Arrays.asList("/__admin/*");
registration.setUrlMappings(urlMappings);
return registration;
}
}
I could see that the Spring-boot has an ambiguity in selecting the Handler class during initialization and picks up the first available ServletRegistrationBean in our case the one pointing to the root /* path and trying to match the /admin/mappings against that URL. My question is how to make Spring-Boot register two Handler classes based on the URL path. This kind of configuration could be handled using a web.xml in regular Spring but I am facing difficulty in making Spring-boot work that way.
来源:https://stackoverflow.com/questions/58348065/spring-boot-wiremock-server-request-was-not-matched-error