Swagger UI - 如何默认展开所有操作?

Swagger UI - How can I expand all the operations by default?

打开时所有操作都是折叠的,我希望它默认展开。

有什么属性我需要改变才能实现吗?

这是我的招摇豆:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(regex("/api/.*"))
                .build()
                .directModelSubstitute(XMLGregorianCalendar.class, Date.class)
                .apiInfo(apiInfo())                
                .useDefaultResponseMessages(false);
    }
}

我相信你可以在创建swagger-ui时设置docExpansion:"full"

详情见https://github.com/swagger-api/swagger-ui#parameters

docExpansion: Controls the default expansion setting for the operations and tags. It can be 'list' (expands only the tags), 'full' (expands the tags and operations) or 'none' (expands nothing). The default is 'list'.

private static final String DOC_EXPANSION = "list"; //none, full

    @Bean
    public UiConfiguration uiConfig() {
        return new UiConfiguration(
                null, DOC_EXPANSION, "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, null
        );
    }

我通过在 swaggerUi 中添加所需的更改来做到这一点。 您需要根据您的要求进行如下更改;

  1. docExpansion : "none" - 它会隐藏所有内容。
  2. docExpansion : "list"- 它只会 expand/List 所有操作。
  3. docExpansion : "full" - 它会展开所有内容(如名称所示完全展开)。

这是您似乎使用的 Springfox 的答案:

  @Bean
  UiConfiguration uiConfig() {
    return UiConfigurationBuilder.builder()
        .docExpansion(DocExpansion.LIST) // or DocExpansion.NONE or DocExpansion.FULL
        .build();
  }  

来源:http://springfox.github.io/springfox/docs/current/#springfox-swagger2-with-spring-mvc-and-spring-boot

我刚刚发现我实际上可以像这样将参数传递给 swagger url:

http://...../swagger/index.html?docExpansion=none

docExpansion : "none" - hide everything.

docExpansion : "list"- expand/List all the operations only.

docExpansion : "full" - expand everything (full expand as the name says).