不用说,我知道关于同一主题有很多问题,但我已经尝试解决这个问题两天了,而且我读到的大部分内容都已经过时了。
所以是的..这就是我现在所拥有的:
Gradle :
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 上工作)。
有很多这样的教程,他们声称上面的内容将使 swagger-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
还有大量其他教程,它们指导您将 dist 文件夹从 swagger-ui git 添加到您的项目。
也像这样映射:
@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/");
}
}
同样失败,抛出 Scope 'request' is not active for the current thread;异常。
在尝试了 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/");
}
}
最佳答案
这就是我们用作设置的内容。一个用@Configuration注释的自己的类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error")))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"APP NAME",
"APP DESCRIPTION",
"1.0",
null,
new Contact("Firstname Lastname", null, "firstname.lastname@somewhere.net"),
null,
null);
} }
关于java - Spring Swagger-ui 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40802015/