Java集成swagger文档组件
程序员文章站
2022-06-27 10:06:25
一:简介 swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 restful 风格的 web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新...
一:简介
swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 restful 风格的 web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许api来始终保持同步。swagger 让部署管理和使用功能强大的api从未如此简单。
二:集成swagger
1.引入pom.xml文件包(导入4个jar包)
注意:jdk1.8以上才能运行swagger2
<!--swagger--> <dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger2</artifactid> <version>2.8.0</version> </dependency> <!--swagger-ui--> <dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger-ui</artifactid> <version>2.8.0</version> </dependency> <!--swagger-ui增强--> <dependency> <groupid>com.github.xiaoymin</groupid> <artifactid>knife4j-spring-boot-starter</artifactid> <version>2.0.4</version> </dependency> <!--swagger-xml bind--> <dependency> <groupid>javax.xml.bind</groupid> <artifactid>jaxb-api</artifactid> <version>2.3.0</version> </dependency>
2.要想使用swagger,必须编写一个配置类来配置 swagger,这里的配置类如下
@configuration @enableswagger2 public class swaggerconfig { private string title = "标题.."; private string description = ""; private string termsofserviceurl = ""; private string version = "版本号.."; @bean public docket createdefaultrestapi() { return new docket(documentationtype.swagger_2).apiinfo(apiinfo()).select() .apis(requesthandlerselectors.withclassannotation(api.class)) .apis(requesthandlerselectors.withmethodannotation(apioperation.class)).paths(pathselectors.any()) .build().groupname("default").securityschemes(securityschemes()).securitycontexts(securitycontexts()); } @bean public docket createtestrestapi() { return new docket(documentationtype.swagger_2).apiinfo(apiinfo()).select() .apis(requesthandlerselectors.withclassannotation(api.class)) .apis(requesthandlerselectors.withmethodannotation(apioperation.class)) .paths(pathselectors.ant("/test/**")).build().groupname("测试/调试").securityschemes(securityschemes()) .securitycontexts(securitycontexts()); } private apiinfo apiinfo() { return new apiinfobuilder().title(title).description(description).termsofserviceurl(termsofserviceurl) .version(version).build(); } private list<securityreference> defaultauth() { authorizationscope authorizationscope = new authorizationscope("global", "accesseverything"); authorizationscope[] authorizationscopes = new authorizationscope[1]; authorizationscopes[0] = authorizationscope; return newarraylist(new securityreference("token", authorizationscopes)); } private list<securitycontext> securitycontexts() { return newarraylist(securitycontext.builder().securityreferences(defaultauth()) .forpaths(pathselectors.regex("^(?!auth).*$")).build()); } private list<apikey> securityschemes() { return newarraylist(new apikey("token", "token", "header")); } }
3.集成restful风格接口示例
@api(tags = "测试") @restcontroller public class testcontroller { @apioperation("get方法") @getmapping("getinfo") public void getinfo(){ } }
4.控制台打印路径地址(可选配置)
@slf4j @springbootapplication public class springbootapplication implements applicationrunner { public static void main(string[] args) { springapplication.run(springbootapplication.class, args); } @autowired environment environment; @override public void run(applicationarguments args) throws exception { log.info("项目已启动,端口:" + environment.getproperty("local.server.port")); log.info("swagger文档地址:http://localhost:" + environment.getproperty("local.server.port") + "/swagger-ui.html"); log.info("swagger文档地址:http://localhost:" + environment.getproperty("local.server.port") + "/doc.html"); } }
三:配置运用swagger
1. http://ip:port/swagger-ui.html
2. http://ip:port/doc.html
到此这篇关于java集成swagger文档组件的文章就介绍到这了,更多相关java集成swagger内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
Springboot集成接口文档swagger,使用,测试
-
SpringBoot集成Swagger2构建在线API文档的代码详解
-
SpringBoot同时集成swagger与knife4j2种API文档
-
Java开发Swagger集成方法以及常见问题
-
详解java如何集成swagger组件
-
[Swagger] Spring MVC 组件配置 之 RESTFUL API文档以及Mock应用(springfox-swagger)
-
go Echo框架集成Swagger 自动生成api文档
-
Spring mvc + mybatis+maven集成swagger ui自动生成api文档
-
Java开发SpringBoot集成接口文档实现示例
-
java集成开发SpringBoot生成接口文档示例实现