首先遵循SpringBoot的三板斧
第一步添加依賴
<!-- SwaggerUI 介面檔案 http://{ip}:{prot}/swagger-ui.html -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>{version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>{version}</version>
</dependency>
第二步添加注解
@EnableSwagger2 //啟動SwaggerUI,在啟動類或Swagger配置類上添加該注解
第三步寫配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
/*
//可以添加多個header或引數
ParameterBuilder aParameterBuilder = new ParameterBuilder();
aParameterBuilder
//引數型別支持header, cookie, body, query etc
.parameterType("header")
//引數名
.name("user-token")
//默認值
.defaultValue("t122222")
.description("用戶登錄憑證")
//指定引數值的型別
.modelRef(new ModelRef("string"))
//非必需,這里是全域配置
.required(false).build();
List<Parameter> aParameters = new ArrayList<>();
aParameters.add(aParameterBuilder.build());
*/
return new Docket(DocumentationType.SWAGGER_2)
// return new Docket(DocumentationType.SPRING_WEB)
.apiInfo(apiInfo())
.pathMapping("/")
.select()// 選擇那些路徑和api會生成document
.apis(RequestHandlerSelectors.any())// 對所有api進行監控
// 不顯示錯誤的介面地址
.paths(Predicates.not(PathSelectors.regex("/error.*")))// 錯誤error路徑不監控
.paths(Predicates.not(PathSelectors.regex("/actuator.*")))// 錯誤error路徑不監控
.paths(PathSelectors.regex("/.*"))// 對根下所有路徑進行監控
.paths(PathSelectors.any()) // 對所有路徑進行監控
// 自行修改為自己的包路徑
// .apis(RequestHandlerSelectors.basePackage("com.happyloves.zc.service.account.api"))
.build()
// .globalOperationParameters(aParameters)
.enable(true);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API介面")
.description("API介面檔案")
//服務條款網址
// .termsOfServiceUrl("https://www.google.com")
.version("1.0")
// .contact(new Contact("啦啦啦", "url", "email"))
.build();
}
}
擴展:swagger-bootstrap-ui是springfox-swagger的增強UI實作,為Java開發者在使用Swagger的時候,能擁有一份簡潔、強大的介面檔案體驗
專案地址
碼云:https://gitee.com/xiaoym/swagger-bootstrap-ui
GitHub:https://github.com/xiaoymin/Swagger-Bootstrap-UI
在線體驗:http://swagger-bootstrap-ui.xiaominfo.com/doc.html
專案檔案:http://www.xiaominfo.com/swagger-bootstrap-ui/
代碼集成示例
SpringBoot在線demo地址:https://gitee.com/xiaoym/swagger-bootstrap-ui-demo
Spring Mvc在線demo地址:https://gitee.com/xiaoym/swagger-bootstrap-ui-demo/tree/master/swagger-bootstrap-ui-demo-mvc
添加依賴
<!-- swagger-bootstrap-ui是 Swagger 的增強UI 實作,使檔案更友好一點兒 http://{ip}:{prot}/doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
趙小胖個人博客:https://zc.happyloves.cn:4443/wordpress/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/249700.html
標籤:Java
上一篇:Java流程控制
下一篇:注意!PHP中字串與數字的比較
