Java集成swagger文档组件
一:简介
??Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
二:集成swagger
1.引入pom.xml文件包(导入4个jar包)
注意:jdk1.8以上才能运行swagger2
io.springfox springfox-swagger22.8.0 io.springfox springfox-swagger-ui2.8.0 com.github.xiaoymin knife4j-spring-boot-starter2.0.4 javax.xml.bind jaxb-api2.3.0
2.要想使用Swagger,必须编写一个配置类来配置 Swagger,这里的配置类如下
@Configuration@EnableSwagger2public class SwaggerConfig {private String title = "标题.."; private String description = ""; private String termsOfServiceUrl = ""; private String version = "版本号.."; @Beanpublic 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()); }@Beanpublic 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 defaultAuth() {AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return newArrayList(new SecurityReference("token", authorizationScopes)); }private List securityContexts() {return newArrayList(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("^(?!auth).*$")).build()); }private List securitySchemes() {return newArrayList(new ApiKey("token", "token", "header")); }}
3.集成RESTful风格接口示例
@Api(tags = "测试")@RestControllerpublic class TestController {@ApiOperation("get方法")@GetMapping("getInfo")public void getInfo(){}}
4.控制台打印路径地址(可选配置)
@Slf4j@SpringBootApplicationpublic class SpringbootApplication implements ApplicationRunner {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args); }@AutowiredEnvironment environment; @Overridepublic 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文档组件的文章就介绍到这了,更多相关Java集成swagger内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Activiti(一)SpringBoot2集成Activiti6
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- 私有化轻量级持续集成部署方案--03-部署web服务(下)
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)