【问题标题】:Can I change the default definition displayed by Swagger?我可以更改 Swagger 显示的默认定义吗?
【发布时间】:2020-02-20 18:01:27
【问题描述】:

我可以将默认定义从“默认”更改为我自己的吗?我希望页面加载,而不是加载“默认”,而是加载我的页面,在这种情况下,它被称为“swagger”:

我正在使用 Spring Fox 和 Spring Boot。这是我的 Swagger Config 类:

@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SwaggerDocumentationConfig {
    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.openet.usage.trigger"))
                .paths(PathSelectors.any())
                .build();
    }

    private static Predicate<String> matchPathRegex(final String... pathRegexs) {
        return new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                for (String pathRegex : pathRegexs) {
                    if (input.matches(pathRegex)) {
                        return true;
                    }
                }
                return false;
            }
        };
    }

    @Bean
    WebMvcConfigurer configurer () {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addResourceHandlers (ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/config/swagger.json").
                        addResourceLocations("classpath:/config");
                registry
                        .addResourceHandler("swagger-ui.html")
                        .addResourceLocations("classpath:/META-INF/resources/");
                registry
                        .addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/");
            }
        };
    }
}

【问题讨论】:

    标签: java spring-boot swagger-ui swagger-2.0 springfox


    【解决方案1】:

    可以更改此行为,但它看起来更像是一种 hack。

    SwaggerResourcesProvider 负责为下拉列表提供信息。首先,实现这个接口。其次,将Primary 注解添加到您的类中,使其成为应该使用的主要实现,而不是默认的InMemorySwaggerResourcesProvider 类。但是重用 InMemorySwaggerResourcesProvider 提供的定义仍然有意义,这就是为什么应该注入它。

    最后一部分是实现重写的get 方法,并更改为要显示的列表。此示例应仅显示一个名为 swagger 的定义。

    // other annotations
    @Primary
    public class SwaggerDocumentationConfig implements SwaggerResourcesProvider { 
       private final InMemorySwaggerResourcesProvider resourcesProvider;
    
       @Inject
       public MySwaggerConfig(InMemorySwaggerResourcesProvider resourcesProvider) {
           this.resourcesProvider = resourcesProvider;
       }
    
       @Override
       public List<SwaggerResource> get() {
           return resourcesProvider.get().stream()
               .filter(r -> "swagger".equals(r.getName()))
               .collect(Collectors.toList());
       }
    
       // the rest of the configuration
    }
    

    【讨论】:

      【解决方案2】:

      我刚刚在我的控制器中做了一个重定向:

      @RequestMapping(value = "/", method = RequestMethod.GET)
      public void redirectRootToSwaggerDocs(HttpServletResponse response) throws IOException {
          response.sendRedirect("/my-api/swagger-ui.html?urls.primaryName=swagger");
      }
      

      【讨论】:

        【解决方案3】:
        ...
        return new Docket(DocumentationType.OAS_30)
                .groupName("<what u want>")
        ...
        

        只需设置一个默认组名。

        【讨论】:

          【解决方案4】:

          我发现最简单的方法就是让 groupName 按字母顺序排列。如“1 swagger”、“a swagger”或“-> swagger”。

          ...
          return new Docket(DocumentationType.OAS_30)
              .groupName("-> swagger");
          ...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-21
            • 2013-10-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多