java - Spring Swagger-ui 集成

标签 java spring swagger-ui swagger-2.0 springfox

不用说,我知道关于同一主题有很多问题,但我已经尝试解决这个问题两天了,而且我读到的大部分内容都已经过时了。

所以是的..这就是我现在所拥有的:

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/

相关文章:

java - 将 @RequestParam 作为列表是不可能的吗?

java - Swagger 中的重复条目

spring - Kafka 监听器的 Swagger 文档

java - 为什么 Kotlin 使用 arrayOf 这样的全局函数

java - 如何正确实现一个spring-websocket java客户端

java - 多次重装JDK后出现JNL错误

spring - 执行 spring 事务时出错

java - Swagger Spring API - xmlModelPlugin 错误

java - XML 转换失败

java - 使用 Pojo 而不是 Rest Controller 时 Spring 服务注入(inject)为 Null