2016-11-15 227 views
1

安靜的非常直接的問題:我如何在v2.2.6中對swagger-ui中的端點進行排序?我正在使用springfox的Java部分。Swagger UI:如何自定義排序資源

在我的主要頁面上,我收集了按標籤分組的資源。這些標籤是隨機排列的。我想按照自定義順序(如果不可能,按字母順序)。 我SwaggerConfig.java文件:

package cat.meteo.apiinterna.commons.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import springfox.documentation.builders.ApiInfoBuilder; 
import springfox.documentation.builders.PathSelectors; 
import springfox.documentation.builders.RequestHandlerSelectors; 
import springfox.documentation.service.ApiInfo; 
import springfox.documentation.service.Tag; 
import springfox.documentation.spi.DocumentationType; 
import springfox.documentation.spring.web.plugins.Docket; 
import springfox.documentation.swagger2.annotations.EnableSwagger2; 

@Configuration 
@EnableSwagger2 
public class SwaggerConfig { 

@Bean 
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2) 
      .select() 
      .apis(RequestHandlerSelectors.any()) 
      .paths(PathSelectors.any()) 
      .build() 
      .tags(
        new Tag("XEMA Me", "1"), 
        new Tag("XEMA Ul", "2"), 
        new Tag("XEMA Ag", "3"), 
        new Tag("Prono", "4"), 
        new Tag("Sound", "5") 
      ) 
      .apiInfo(apiInfo()); 
} 

private ApiInfo apiInfo() { 
    return new ApiInfoBuilder() 
      .title("API REST") 
      .description("Self-documented API") 
      .version("v0.1.0") 
      .build(); 
    } 
} 

這是招搖的用戶界面以相同的順序使用標籤生成的JSON文件。正如你所看到的順序似乎是隨機的。不是java標籤的順序,而不是按字母順序。 (http://localhost:8080/XXX/v2/api-docs

"tags": [ 
{ 
"name": "XEMA Ul", 
"description": "2" 
}, 
{ 
"name": "XEMA Me", 
"description": "1" 
}, 
{ 
"name": "XEMA Ag", 
"description": "3" 
}, 
{ 
"name": "Sound", 
"description": "5" 
}, 
{ 
"name": "Prono", 
"description": "4" 
} 
] 

回答

2

挖掘到這個問題後,我看到招搖的UI的新版本不支持自定義排序作爲一個選項。 Springfox也沒有給生成的json重新排序的機會,所以唯一的方法是在swagger-ui中實現「解決方法」。

在渲染功能,線21766招搖,ui.js文件(V2.2.6)的:

// Workaround: alphabetically ordered tags 
this.api.apisArray.sort(function (a, b) { 
if (a.tag < b.tag) 
    return -1; 
if (a.tag > b.tag) 
    return 1; 
return 0; 
}) 

這段代碼的UI顯示命令標籤。

+0

請參閱https://github.com/springfox/springfox/issues/732 – splintor