我有自己的RequestMappingHandlerMapping,正在使用springfox-swagger-ui。添加我的自定义映射后,我无法在http://localhost:8080/swagger-ui.html处获得swagger ui。有什么想法吗?
这是我的配置。
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new ApiVersionRequestMappingHandlerMapping("v");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("(META-INF/resources/webjars");
}
}
这是我的pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
[当您覆盖WebMvcConfigurationSupport时,您还将覆盖Spring Boot的mvc自动配置(WebMvcAutoConfiguration)。因此,需要spring boot配置的资源将无法使用。这不是招摇不定的问题。
您可以在这里找到有关此的更多信息:
https://github.com/spring-projects/spring-boot/issues/5004
正如github问题所暗示的那样,将来会对此进行更改以使其更容易。当前有一些变通方法,如建议的那样。
一种快速而肮脏的方法是,将WebMvcAutoConfiguration类复制并粘贴到您自己的类中,从EnableWebMvcConfiguration的requestMappingHandlerMapping()方法返回您自己的HandlerMapping,并将WebMvcAutoConfiguration的副本注册为自动配置类。您可以在此处查看说明:
请确保将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;
}
覆盖requestMappingHandlerMapping()
的WebMvcConfigurationSupport
将关闭Spring Boot的自动配置。要添加自定义MVC组件,可以使用WebMvcRegistrations。就像提供自定义RequestMappingHandlerMapping
一样,我们可以使用自定义getRequestMappingHandlerMapping()
覆盖RequestMappingHandlerMapping
的WebMvcRegistrationsAdapter
,并通过webMvcRegistrationsHandlerMapping()
提供它。作为,
@Configuration
class CustomRequestMappingHandlerMapping {
@Bean
public WebMvcRegistrationsAdapter webMvcRegistrationsHandlerMapping() {
return new WebMvcRegistrationsAdapter() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new ApiVersionRequestMappingHandlerMapping("v");
}
};
}
}
它对我有用。覆盖addResourceHandlers而不是注册自动配置。
@Configuration
@EnableWebMvc
@EnableSwagger2
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
在Spring Boot 2.0.0中,有一种更简单的方法可以实现此目的。
将WebMvcRegistrations
接口的实例创建为Bean,并覆盖适当的方法以返回该对象的自定义版本。 Spring Boot将读取并使用该实例。
在这种情况下,只需要覆盖getRequestMappingHandlerMapping()
并返回自定义实现
以上信息来自@Nazaret K提供的链接。https://github.com/spring-projects/spring-boot/issues/5004的更多信息
可以通过使用WebMvcConfigurationSupport并为swagger添加资源处理程序来解决:
@Configuration
public class MvcConfiguration extends WebMvcConfigurationSupport {
@Value("${spring.application.name}")
private String applicationName;
//...irrelevant code here
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
我终于找到了!正确的配置是这样的:
@Configuration
public class VersioningMappingHandlerConfig {
@Bean
public ApiVersionRequestMappingHandlerMapping customMappingHandlerMapping() {
ApiVersionRequestMappingHandlerMapping handler = new ApiVersionRequestMappingHandlerMapping("v", 1, 1);
handler.setOrder(-1);
return handler;
}
}
注意:没有extends WebMvcConfigurationSupport
,bean名称是customMappingHandlerMapping