当你重写WebMvcConfigurationSupport时,你也重写了spring Boot的mvc自动配置(WebMvcAutoConfiguration)。因此,需要spring boot配置的资源将无法工作。这不是 swagger 特有的问题。
您可以在这里找到更多相关信息:
https://github.com/spring-projects/spring-boot/issues/5004 https://github.com/spring-projects/spring-boot/issues/5004
正如 github 问题所表明的那样,将来将会对此进行更改以使其更容易。目前有一些解决方法,如建议的那样。
一种快速但肮脏的方法是将 WebMvcAutoConfiguration 类复制并粘贴到您自己的类中,从 EnableWebMvcConfiguration 的 requestMappingHandlerMapping() 方法返回您自己的 HandlerMapping,并将 WebMvcAutoConfiguration 的副本注册为自动配置类。您可以在此处查看说明:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html
确保将 WebMvcAutoConfiguration 的副本放置在某个未自动扫描和拾取组件的包中。它应该按照上面链接中的说明进行注册。
还要确保在从 requestMappingHandlerMapping() 方法返回自定义 HandlerMapping() 方法之前将其顺序设置为 0,如下所示:
@Bean
@Primary
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
// Must be @Primary for MvcUriComponentsBuilder to work
ApiVersionRequestMappingHandlerMapping handlerMapping = new ApiVersionRequestMappingHandlerMapping("v");
handlerMapping.setOrder(0);
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager());
PathMatchConfigurer configurer = getPathMatchConfigurer();
if (configurer.isUseSuffixPatternMatch() != null) {
handlerMapping.setUseSuffixPatternMatch(configurer.isUseSuffixPatternMatch());
}
if (configurer.isUseRegisteredSuffixPatternMatch() != null) {
handlerMapping.setUseRegisteredSuffixPatternMatch(configurer.isUseRegisteredSuffixPatternMatch());
}
if (configurer.isUseTrailingSlashMatch() != null) {
handlerMapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch());
}
if (configurer.getPathMatcher() != null) {
handlerMapping.setPathMatcher(configurer.getPathMatcher());
}
if (configurer.getUrlPathHelper() != null) {
handlerMapping.setUrlPathHelper(configurer.getUrlPathHelper());
}
return handlerMapping;
}