将swagger与mvc集成并使用基于java的配置(如
)@Configuration
@EnableSwagger2
@PropertySource("classpath:application.properties")
public class SwaggerConfig 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/");
}
}
并使用网址http://localhost:8080/admin-api/admin/swagger-ui.html
,它会给出404。
当我从addResourceHandlers
配置类中删除SwaggerConfig
并通过xml配置
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
使用相同的网址http://localhost:8080/admin-api/admin/swagger-ui.html
,它运行没有问题。我该如何使用基于java的配置?
答案 0 :(得分:0)
swagger - 基于java的配置 一个类来自WebMvcConfigurerAdapter,在该类文件中通过覆盖 addResourceHandlers ,如下所示
@Configuration
public class AppConfig 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/");
super.addResourceHandlers(registry);
}
}
答案 1 :(得分:0)
尝试使用@EnableWebMvc举报SwaggerConfig
并将"**/**"
注册为另一个ResourceHandler,如下所示:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("**/**").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
另请参阅Docket,这是使用Docket API的示例应用程序的链接 - swagger-example。