SpringBoot整合Swagger2代碼實(shí)例
首先遵循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-swaggerui</artifactId> <version>{version}</version> </dependency>
第二步添加注解
@EnableSwagger2 //啟動SwaggerUI,在啟動類或Swagger配置類上添加該注解
第三步寫配置
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { /* //可以添加多個header或參數(shù) ParameterBuilder aParameterBuilder = new ParameterBuilder(); aParameterBuilder //參數(shù)類型支持header, cookie, body, query etc .parameterType("header") //參數(shù)名 .name("user-token") //默認(rèn)值 .defaultValue("t122222") .description("用戶登錄憑證") //指定參數(shù)值的類型 .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進(jìn)行監(jiān)控 // 不顯示錯誤的接口地址 .paths(Predicates.not(PathSelectors.regex("/error.*")))// 錯誤error路徑不監(jiān)控 .paths(Predicates.not(PathSelectors.regex("/actuator.*")))// 錯誤error路徑不監(jiān)控 .paths(PathSelectors.regex("/.*"))// 對根下所有路徑進(jìn)行監(jiān)控 .paths(PathSelectors.any()) // 對所有路徑進(jìn)行監(jiān)控 // 自行修改為自己的包路徑 // .apis(RequestHandlerSelectors.basePackage("com.happyloves.zc.service.account.api")) .build() // .globalOperationParameters(aParameters) .enable(true); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API接口") .description("API接口文檔") //服務(wù)條款網(wǎng)址 // .termsOfServiceUrl("https://www.google.com") .version("1.0") // .contact(new Contact("啦啦啦", "url", "email")) .build(); } }
擴(kuò)展:swagger-bootstrap-ui是springfox-swagger的增強(qiáng)UI實(shí)現(xiàn),為Java開發(fā)者在使用Swagger的時候,能擁有一份簡潔、強(qiáng)大的接口文檔
體驗(yàn)項(xiàng)目地址碼云:https://gitee.com/xiaoym/swagger-bootstrap-ui
GitHub: https://github.com/xiaoymin/Swagger-Bootstrap-UI
在線體驗(yàn):http://swagger-bootstrap-ui.xiaominfo.com/doc.html
代碼集成示例
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 的增強(qiáng)UI 實(shí)現(xiàn),使文檔更友好一點(diǎn)兒 http://{ip}:{prot}/doc.html --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus動態(tài)數(shù)據(jù)源讀寫分離方式
在分布式項(xiàng)目開發(fā)中,動態(tài)數(shù)據(jù)源的配置與使用至關(guān)重要,通過創(chuàng)建DynamicDatasourceService,實(shí)現(xiàn)數(shù)據(jù)源的動態(tài)添加與調(diào)用,有效管理主從庫操作,減輕數(shù)據(jù)庫壓力,此外,通過配置類與@DS注解,實(shí)現(xiàn)了靈活的分庫查詢功能,為高效處理數(shù)據(jù)提供了強(qiáng)有力的支持2024-10-10Java數(shù)據(jù)結(jié)構(gòu)之簡單的連接點(diǎn)(link)實(shí)現(xiàn)方法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之簡單的連接點(diǎn)(link)實(shí)現(xiàn)方法,涉及java指針指向節(jié)點(diǎn)的相關(guān)使用技巧,需要的朋友可以參考下2017-10-10IDEA使用maven創(chuàng)建hibernate項(xiàng)目的實(shí)現(xiàn)步驟(圖文)
本文主要介紹了IDEA使用maven創(chuàng)建hibernate項(xiàng)目的實(shí)現(xiàn)步驟,包括創(chuàng)建Maven項(xiàng)目,配置Hibernate,以及創(chuàng)建實(shí)體類映射到數(shù)據(jù)庫等步驟,具有一定的參考價值,感興趣的可以了解一下2023-08-08SpringBoot中使用MyBatis-Plus實(shí)現(xiàn)分頁接口的詳細(xì)教程
MyBatis-Plus是一個MyBatis的增強(qiáng)工具,在MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,在SpringBoot項(xiàng)目中使用MyBatis-Plus可以大大簡化分頁邏輯的編寫,本文將介紹如何在 SpringBoot項(xiàng)目中使用MyBatis-Plus實(shí)現(xiàn)分頁接口2024-03-03Java使用選擇排序法對數(shù)組排序?qū)崿F(xiàn)代碼
這篇文章主要介紹了Java使用選擇排序法對數(shù)組排序?qū)崿F(xiàn)代碼,需要的朋友可以參考下2014-02-02Java 迪杰斯特拉算法實(shí)現(xiàn)查找最短距離的實(shí)現(xiàn)
這篇文章主要介紹了Java 迪杰斯特拉算法實(shí)現(xiàn)查找最短距離的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09