Java Swagger技術(shù)使用指南
Swagger的作用與概念
Swagger官網(wǎng),點此進入
在前后端分離時代,我們需要實時自動更新接口信息,和測試接口,實現(xiàn)前后端分離式開發(fā),swagger因此產(chǎn)生
在項目中使用swagger
以下以3.0.0依賴為例
<!--swagger 相關(guān)組件--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
@RestController public class HelloController { @RequestMapping(value = "/hello") public String hello(){ return "hello"; } }
@Configuration @EnableSwagger2 //開啟swagger2 public class SwaggerConfig { }
然后訪問http://localhost:8080/swagger-ui/index.html
你就能看到如下界面,為swagger文檔
配置swagger
先來看看底層的代碼,了解一下
ApiInfo 配置
@Configuration @EnableSwagger2 //開啟swagger2 public class SwaggerConfig { @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } //配置swagger信息apiInfo private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact("宋先慧", "https://blog.csdn.net/sxh06", "xianhuisong@yeah.net"); return new ApiInfo( "宋先慧的Api Documentation", "學(xué)習(xí)swagger沒有盡頭", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
swagger配置掃描接口
@Configuration @EnableSwagger2 //開啟swagger2 public class SwaggerConfig { // @Bean // public Docket docket1(){ // return new Docket(DocumentationType.SWAGGER_2).groupName("分組二"); // } @Bean public Docket docket(Environment environment){ Profiles profiles=Profiles.of("dev"); //獲取項目的環(huán)境 boolean flag=environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("宋先慧") //分組 .enable(flag) //enable 配置是否啟動swagger flase則不能在瀏覽器訪問 .select() //RequestHandlerSelectors實現(xiàn)類 配置掃描方式 // basePackage指定要掃描的包 // any()全部 // none()都不掃描 //withClassAnnotation() 掃描類上的注解 參數(shù)是一個注解的反射對象 //withMethodAnnotation 掃描方法上的注解 .apis(RequestHandlerSelectors.basePackage("com.sxh.swagger.controller")) //.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class)) //過濾什么路勁 過濾請求 //.paths(PathSelectors.ant("/sxh/**")) .build(); } //配置swagger信息apiInfo private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact("宋先慧", "https://blog.csdn.net/sxh06", "xianhuisong@yeah.net"); return new ApiInfo( "宋先慧的Api Documentation", "學(xué)習(xí)swagger沒有盡頭", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
如果我只希望在生成環(huán)境使用swagger,在正式環(huán)境不使用swagger怎么解決?(enable=false|true)
配置api文檔分組
多個分組
配置多個Docket 實例即可
@Bean public Docket docket1(){ return new Docket(DocumentationType.SWAGGER_2).groupName("分組一"); } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2).groupName("分組二"); }`
實體類配置
到此這篇關(guān)于Java Swagger技術(shù)使用指南的文章就介紹到這了,更多相關(guān)Java Swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot3整合 Elasticsearch 8.x 使用Repository構(gòu)
我們構(gòu)建了一個完整的 Spring Boot 3 和 Elasticsearch 8.x 的增刪改查示例應(yīng)用,使用 Spring Data Elasticsearch Repository,我們能夠快速實現(xiàn)對 Elasticsearch 的基本 CRUD 操作,簡化了開發(fā)流程,希望這個示例能夠幫助你理解如何在項目中有效使用 Elasticsearch!2024-11-11SpringBoot?+DynamicDataSource切換多數(shù)據(jù)源的全過程
這篇文章主要介紹了SpringBoot?+DynamicDataSource切換多數(shù)據(jù)源的全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Spring sentinel哨兵模式相關(guān)原理解析
這篇文章主要介紹了Spring sentinel哨兵模式相關(guān)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11SpringBoot整合RocketMq實現(xiàn)分布式事務(wù)
這篇文章主要為大家詳細介紹了SpringBoot整合RocketMq實現(xiàn)分布式事務(wù)的相關(guān)知識,文中的示例代碼講解詳細,有需要的小伙伴可以參考一下2024-11-11在Java中避免NullPointerException的解決方案
這篇文章主要介紹了在Java中避免NullPointerException的解決方案,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04如何解決Could not transfer artifact org.spri
在Maven更新過程中遇到“Could not transfer artifact org.springframework.boot”錯誤通常是由于網(wǎng)絡(luò)問題,解決方法是在Maven的設(shè)置中忽略HTTPS,添加特定語句后,可以正常下載依賴,但下載速度可能較慢,這是一種常見的解決方案,希望對遇到相同問題的人有所幫助2024-09-09