您好,登錄后才能下訂單哦!
這篇文章給大家介紹Springboot項(xiàng)目接口之swagger怎么用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
step1:pom文件引入swagger插件
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
step2:添加SwaggerConfig.java
package com.ggkt.crm.admin.config; import org.springframework.beans.factory.annotation.Value; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Title: Swagger * Description:接口測試(可用、可不用) * @author ggkt * */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Value(value = "${swagger.enabled}") private Boolean swaggerEnabled; @Bean public Docket createAllotManageApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .groupName("分配管理") // 是否開啟 .enable(swaggerEnabled).select() // 掃描的路徑包 .apis(RequestHandlerSelectors.basePackage("com.ggkt.crm.admin.allotManage.dataType.controller")) // 指定路徑處理PathSelectors.any()代表所有的路徑 .paths(PathSelectors.any()).build().pathMapping("/"); } @Bean public Docket createMarketManageApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .groupName("營銷管理") // 是否開啟 .enable(swaggerEnabled).select() // 掃描的路徑包 .apis(RequestHandlerSelectors.basePackage("com.ggkt.crm.admin.marketManage")) // 指定路徑處理PathSelectors.any()代表所有的路徑 .paths(PathSelectors.any()).build().pathMapping("/"); } @Bean public Docket createBaseManageApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .groupName("基礎(chǔ)配置管理") // 是否開啟 .enable(swaggerEnabled).select() // 掃描的路徑包 .apis(RequestHandlerSelectors.basePackage("com.ggkt.crm.admin.baseManage")) // 指定路徑處理PathSelectors.any()代表所有的路徑 .paths(PathSelectors.any()).build().pathMapping("/"); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("接口文檔") .description("API") .version("1.0.0") .build(); } }
step3:domain類注釋方式
step4:Controller類的注釋方式
step5:application.yml配置
6、接口訪問
http://localhost:9099/admin/swagger-ui.html
關(guān)于Springboot項(xiàng)目接口之swagger怎么用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。