Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603788
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-05-23 01:00:22


点击(此处)折叠或打开


  1. 1. 引入依赖,版本 3.0.0 只引入一个即可

  2.         <dependency>
  3.          <groupId>io.springfox</groupId>
  4.          <artifactId>springfox-boot-starter</artifactId>
  5.          <version>3.0.0</version>
  6.         </dependency>
  7. 2. 配置类 SwaggerConfig

  8. package org.fh.config;

  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.List;

  12. import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
  13. import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
  14. import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
  15. import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
  16. import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
  17. import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
  18. import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
  19. import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
  20. import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
  21. import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
  22. import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
  23. import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
  24. import org.springframework.context.annotation.Bean;
  25. import org.springframework.context.annotation.Configuration;
  26. import org.springframework.core.env.Environment;
  27. import org.springframework.util.StringUtils;

  28. import springfox.documentation.builders.ApiInfoBuilder;
  29. import springfox.documentation.builders.PathSelectors;
  30. import springfox.documentation.builders.RequestHandlerSelectors;
  31. import springfox.documentation.oas.annotations.EnableOpenApi;
  32. import springfox.documentation.service.ApiInfo;
  33. import springfox.documentation.spi.DocumentationType;
  34. import springfox.documentation.spring.web.plugins.Docket;

  35. /**
  36.  * 说明:Swagger 接口API生成
  37.  * 作者:FH Admin Q313596790
  38.  * from:fhadmin.cn
  39.  */
  40. @Configuration
  41. @EnableOpenApi
  42. public class SwaggerConfig {

  43.     @Bean
  44.     public Docket createRestApi() {
  45.         return new Docket(DocumentationType.OAS_30)
  46.                 .apiInfo(apiInfo())
  47.                 .select()
  48.                 .apis(RequestHandlerSelectors.basePackage("org.fh.controller"))    // 为当前包路径
  49.                 .paths(PathSelectors.any())
  50.                 .build();
  51.     }

  52.     private ApiInfo apiInfo() {
  53.         return new ApiInfoBuilder()
  54.                 .title("FH Admin Swagger3 RESTful API")     // 页面标题
  55.                 .version("3.0")                                // 版本号
  56.                 .description("fhadmin.cn")                 // 描述
  57.                 .build();
  58.     }
  59.     
  60.     /**
  61.      * 增加如下配置可解决Spring Boot 2.6.x以上 与Swagger 3.0.0 不兼容问题
  62.     **/
  63.     @Bean
  64.     public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
  65.         List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
  66.         Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
  67.         allEndpoints.addAll(webEndpoints);
  68.         allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
  69.         allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
  70.         String basePath = webEndpointProperties.getBasePath();
  71.         EndpointMapping endpointMapping = new EndpointMapping(basePath);
  72.         boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
  73.         return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
  74.     }
  75.     private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
  76.         return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
  77.     }

  78. }

  79. 3.Swagger 拦截配置

  80. package org.fh.config;
  81.  
  82. import org.springframework.context.annotation.Configuration;
  83. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  84. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  85. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  86.  
  87. /**
  88.  * 说明:Swagger 拦截配置
  89.  * 作者:FH Admin
  90.  * from fhadmin.org
  91.  */
  92. @Configuration
  93. public class WebMvcConfig implements WebMvcConfigurer {
  94.  
  95.     @Override
  96.     public void addResourceHandlers(ResourceHandlerRegistry registry) {
  97.         registry.
  98.                 addResourceHandler("/swagger-ui/**")
  99.                 .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  100.                 .resourceChain(false);
  101.     }
  102.  
  103.     @Override
  104.     public void addViewControllers(ViewControllerRegistry registry) {
  105.         registry.addViewController("/swagger-ui/")
  106.                 .setViewName("forward:/swagger-ui/index.html");
  107.     }
  108. }
  109.  
  110. 4. 访问 127.0.0.1:8081/swagger-ui/index.html

  111. 5. 接口说明案例

  112. 处理类上加注解,比如
  113. @Api("用户注册登录接口")
  114.  
  115. 在方法上加注解,比如
  116. @ApiOperation(value = "登录", notes="请求登录验证用户接口,校验登录是否成功")
  117. @ApiImplicitParam(name = "KEYDATA", value = "用户名密码混淆码组合", paramType = "query", required = true, dataType = "String")


阅读(1527) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~