服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java集成swagger文档组件

Java集成swagger文档组件

2021-09-17 09:40guxiaohai_ Java教程

这篇文章主要介绍了Java集成swagger文档组件,通过详细的图文介绍和代码分析展示,从头展开说明详细的过程,希望对你的开发有所帮助

一:简介

  Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

二:集成swagger

1.引入pom.xml文件包(导入4个jar包)

注意:jdk1.8以上才能运行swagger2

  1. <!--swagger-->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.8.0</version>
  6. </dependency>
  7. <!--swagger-ui-->
  8. <dependency>
  9. <groupId>io.springfox</groupId>
  10. <artifactId>springfox-swagger-ui</artifactId>
  11. <version>2.8.0</version>
  12. </dependency>
  13. <!--swagger-ui增强-->
  14. <dependency>
  15. <groupId>com.github.xiaoymin</groupId>
  16. <artifactId>knife4j-spring-boot-starter</artifactId>
  17. <version>2.0.4</version>
  18. </dependency>
  19. <!--swagger-xml bind-->
  20. <dependency>
  21. <groupId>javax.xml.bind</groupId>
  22. <artifactId>jaxb-api</artifactId>
  23. <version>2.3.0</version>
  24. </dependency>

2.要想使用Swagger,必须编写一个配置类来配置 Swagger,这里的配置类如下

  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfig {
  4. private String title = "标题..";
  5. private String description = "";
  6. private String termsOfServiceUrl = "";
  7. private String version = "版本号..";
  8.  
  9. @Bean
  10. public Docket createDefaultRestApi() {
  11. return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
  12. .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
  13. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
  14. .build().groupName("default").securitySchemes(securitySchemes()).securityContexts(securityContexts());
  15. }
  16.  
  17. @Bean
  18. public Docket createTestRestApi() {
  19. return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
  20. .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
  21. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  22. .paths(PathSelectors.ant("/test/**")).build().groupName("测试/调试").securitySchemes(securitySchemes())
  23. .securityContexts(securityContexts());
  24. }
  25.  
  26. private ApiInfo apiInfo() {
  27. return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl)
  28. .version(version).build();
  29. }
  30.  
  31. private List<SecurityReference> defaultAuth() {
  32. AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
  33. AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
  34. authorizationScopes[0] = authorizationScope;
  35. return newArrayList(new SecurityReference("token", authorizationScopes));
  36. }
  37.  
  38. private List<SecurityContext> securityContexts() {
  39. return newArrayList(SecurityContext.builder().securityReferences(defaultAuth())
  40. .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
  41. }
  42.  
  43. private List<ApiKey> securitySchemes() {
  44. return newArrayList(new ApiKey("token", "token", "header"));
  45. }
  46. }

3.集成RESTful风格接口示例

  1. @Api(tags = "测试")
  2. @RestController
  3. public class TestController {
  4.  
  5. @ApiOperation("get方法")
  6. @GetMapping("getInfo")
  7. public void getInfo(){
  8.  
  9. }
  10. }

4.控制台打印路径地址(可选配置

  1. @Slf4j
  2. @SpringBootApplication
  3. public class SpringbootApplication implements ApplicationRunner {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(SpringbootApplication.class, args);
  7. }
  8.  
  9. @Autowired
  10. Environment environment;
  11.  
  12. @Override
  13. public void run(ApplicationArguments args) throws Exception {
  14. log.info("项目已启动,端口:" + environment.getProperty("local.server.port"));
  15. log.info("swagger文档地址:http://localhost:" + environment.getProperty("local.server.port") + "/swagger-ui.html");
  16. log.info("swagger文档地址:http://localhost:" + environment.getProperty("local.server.port") + "/doc.html");
  17. }
  18. }

三:配置运用swagger

1. http://ip:port/swagger-ui.html

Java集成swagger文档组件

2. http://ip:port/doc.html

Java集成swagger文档组件

到此这篇关于Java集成swagger文档组件的文章就介绍到这了,更多相关Java集成swagger内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/guxiaohai_/article/details/117928391

延伸 · 阅读

精彩推荐