Swagger 是一種非常流行的API文檔工具,提供了許多有用的功能,其中包括自動生成API文檔、API測試、在線接口調試等。通過使用Swagger,開發人員可以更加方便地了解和使用API。默認情況下,Swagger提供了一個默認地址,也就是 ‘/swagger-ui.html’,本文將從多個方面詳細介紹這個默認地址。
一、默認地址的作用
Swagger默認地址 ‘/swagger-ui.html’ 的主要作用是提供API文檔和API測試工具,以幫助開發人員更快地理解和使用API。在默認地址上,開發人員可以看到API的詳細信息、請求和響應的示例、請求參數等重要信息。此外,在默認地址上,開發人員可以進行在線的API測試和接口調試,並獲取實時的響應結果。因此,Swagger默認地址無疑是開發人員必備的工具之一。
二、如何訪問默認地址
要訪問Swagger默認地址 ‘/swagger-ui.html’,只需要在API服務的URL後面添加 ‘/swagger-ui.html’即可,例如:
http://localhost:8080/swagger-ui.html
其中,’localhost:8080′ 是API服務的URL地址,’/swagger-ui.html’ 是Swagger默認地址。需要注意的是,Swagger默認地址只有在API服務部署成功後才能訪問。
三、美化默認地址
默認情況下,Swagger默認地址的界面風格不夠美觀,開發人員可以通過自定義CSS和JavaScript文件來美化Swagger默認地址。下面是一個簡單的示例:
首先,在API服務的靜態資源目錄下新建 ‘swagger’ 文件夾,用於存放自定義的CSS和JavaScript文件。然後,將以下代碼加入到API服務的Spring配置文件中:
@Configuration @EnableSwagger2 @Import(SwaggerUiConfiguration.class) public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot APIs") .description("Spring Boot APIs") .termsOfServiceUrl("") .version("1.0") .build(); } @Autowired private SwaggerUiConfiguration swaggerUiConfig; @PostConstruct public void init() { swaggerUiConfig.setValidatorUrl(null); } }
在上述代碼中,’@Import(SwaggerUiConfiguration.class)’ 用於引入SwaggerUI的配置信息。可以通過修改’ApiInfo’類的屬性,來設置SwaggerUI的標題、描述信息等。最後,通過 ‘PostConstruct’註解,來禁用ValidatorUrl,可以有效的解決SwaggerUI的安全警告。通過這些設置,可以使Swagger默認地址更加美觀和用戶友好。
四、自定義默認地址
除了使用Swagger默認地址之外,開發人員還可以根據需要自定義Swagger的地址。要自定義Swagger地址,只需將以下代碼加入到API服務的Spring配置文件中:
@Configuration @EnableSwagger2 @Import(SwaggerUiConfiguration.class) public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot APIs") .description("Spring Boot APIs") .termsOfServiceUrl("") .version("1.0") .build(); } @Autowired private SwaggerUiConfiguration swaggerUiConfig; @PostConstruct public void init() { swaggerUiConfig.setValidatorUrl(null); } @Bean public Docket testApi() { return new Docket(DocumentationType.SWAGGER_2).groupName("test"); } @Bean public Docket userApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("user") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.user.controller")) .paths(PathSelectors.any()) .build(); } }
在上述代碼中,’createRestApi’ 方法用於設置默認的Swagger文檔詳情信息,’testApi’ 和 ‘userApi’ 方法則用於自定義Swagger文檔詳情信息。其中,groupName用於設置分組名稱,方便用戶查看和使用。通過以上設置,可以方便自如地自定義Swagger默認地址。
五、總結
Swagger默認地址是非常有用的API文檔工具,通過本文的詳細介紹,我們可以更加深入地了解Swagger默認地址的作用和使用方法。在實際開發中,我們可以通過美化默認界面、自定義地址等方式來提高Swagger的使用效率和便捷性。希望本文可以為大家在開發過程中提供幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/301770.html