不用说我知道关于这个主题有很多问题,但我一直在试图解决这个问题2天,现在我阅读的大部分内容都已经过时。Spring Swagger-ui integration
所以啊..这就是我现在所拥有的:
摇篮:
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-hystrix'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.ws:spring-ws-core'
compile 'io.springfox:springfox-swagger2:2.4.0'
compile 'io.springfox:springfox-swagger-ui:2.4.0'
}
主要类:
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(FeedServiceApplication.class, args);
}
@Bean
public Docket swaggerSettings() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/");
}
当我访问:http://localhost:8080/v2/api-docs,我得到的JSON文件正好。另外,当我从github下载swagger-ui时,将源设置为上面的链接并在桌面上运行它,它也可以正常工作。不起作用的是把这两样东西放在一起(让它在http://localhost:8080/swagger-ui.html工作)。
有很多教程,如这些,他们要求上述的东西会让招摇的UI工作:
http://kubecloud.io/guide-using-swagger-for-documenting-your-spring-boot-rest-api/
http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
一吨,他们要你去其他教程从swagger-ui git添加dist文件夹到您的项目中。
此外映射是这样的:
@Configuration
@EnableWebMvc
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/");
}
}
失败,那么投掷范围“请求”不是当前线程活性;例外。
在尝试了一些来自YouTube的教程之后,上面链接了很多教程,我看过的所有内容都是“找不到页面”。如果有人能解释我错过了什么,我会非常感激。
TL:DR如何让swagger-ui.html工作?
编辑:找到了解决方案。
如果有人遇到这种情况,问题是如果你有一个请求参数为@RequestMapping("/{param}")
的请求映射,那么dispatcherServlet不再将/ **映射到ResourceHttpRequestHandler。下面的代码解决了这个问题。
@Configuration
@EnableAutoConfiguration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
@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/");
}
}
你必须使用的swagger-ui是由springfox提供的那个,它使用webjar,因此没有html或css文件可以手动安装到你的webapp中。 –
感谢您的帮助。陷入了同样的问题。 –