这篇文章主要介绍了Java如何集成swagger文档组件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一:简介 Swagger 是一个规范和完整的框架,用于生成、描述、调用
这篇文章主要介绍了Java如何集成swagger文档组件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 WEB 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许api来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
引入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>
要想使用Swagger,必须编写一个配置类来配置 Swagger,这里的配置类如下
@Configuration@EnableSwagger2public 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")); }}
集成RESTful风格接口示例
@Api(tags = "测试")@RestControllerpublic class TestController { @ApiOperation("get方法") @GetMapping("getInfo") public void getInfo(){ }}
控制台打印路径地址(可选配置)
@Slf4j@SpringBootApplicationpublic 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"); }}
http://ip:port/swagger-ui.html
http://ip:port/doc.html
感谢你能够认真阅读完这篇文章,希望小编分享的“Java如何集成swagger文档组件”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: Java如何集成swagger文档组件
本文链接: https://www.lsjlt.com/news/296458.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0