我可以更改Swagger显示的默认定义吗?

时间:2020-02-20 18:01:27

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

enter image description here

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

enter image description here

我正在使用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/");
            }
        };
    }
}

3 个答案:

答案 0 :(得分:1)

可以更改此行为,但看起来更像是黑客。

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
}

答案 1 :(得分:1)

我刚刚在控制器中进行了重定向:

Send

答案 2 :(得分:0)

...
return new Docket(DocumentationType.OAS_30)
        .groupName("<what u want>")
...

只需设置一个默认组名。