这篇文章给大家介绍Springboot项目接口之swagger怎么用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
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("基础配置管理")
// 是否开启
.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
关于Springboot项目接口之swagger怎么用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/guiguketang/blog/5054951