Swagger2Config.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package org.jeecg.config;
  2. import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
  3. import io.swagger.annotations.ApiOperation;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.jeecg.modules.shiro.vo.DefContants;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  10. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  11. import springfox.documentation.builders.ApiInfoBuilder;
  12. import springfox.documentation.builders.ParameterBuilder;
  13. import springfox.documentation.builders.PathSelectors;
  14. import springfox.documentation.builders.RequestHandlerSelectors;
  15. import springfox.documentation.schema.ModelRef;
  16. import springfox.documentation.service.ApiInfo;
  17. import springfox.documentation.service.ApiKey;
  18. import springfox.documentation.service.Parameter;
  19. import springfox.documentation.service.SecurityScheme;
  20. import springfox.documentation.spi.DocumentationType;
  21. import springfox.documentation.spring.web.plugins.Docket;
  22. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. /**
  27. * @Author scott
  28. */
  29. @Slf4j
  30. @Configuration
  31. @EnableSwagger2
  32. @EnableSwaggerBootstrapUI
  33. @ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
  34. public class Swagger2Config implements WebMvcConfigurer {
  35. /**
  36. *
  37. * 显示swagger-ui.html文档展示页,还必须注入swagger资源:
  38. *
  39. * @param registry
  40. */
  41. @Override
  42. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  43. registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
  44. registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
  45. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  46. }
  47. /**
  48. * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
  49. *
  50. * @return Docket
  51. */
  52. @Bean
  53. public Docket createRestApi() {
  54. return new Docket(DocumentationType.SWAGGER_2)
  55. .apiInfo(apiInfo())
  56. .select()
  57. //此包路径下的类,才生成接口文档
  58. .apis(RequestHandlerSelectors.basePackage("org.jeecg.modules"))
  59. //加了ApiOperation注解的类,才生成接口文档
  60. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  61. .paths(PathSelectors.any())
  62. .build()
  63. .securitySchemes(Collections.singletonList(securityScheme()));
  64. //.globalOperationParameters(setHeaderToken());
  65. }
  66. /***
  67. * oauth2配置
  68. * 需要增加swagger授权回调地址
  69. * http://localhost:8888/webjars/springfox-swagger-ui/o2c.html
  70. * @return
  71. */
  72. @Bean
  73. SecurityScheme securityScheme() {
  74. return new ApiKey(DefContants.X_ACCESS_TOKEN, DefContants.X_ACCESS_TOKEN, "header");
  75. }
  76. /**
  77. * JWT token
  78. * @return
  79. */
  80. private List<Parameter> setHeaderToken() {
  81. ParameterBuilder tokenPar = new ParameterBuilder();
  82. List<Parameter> pars = new ArrayList<>();
  83. tokenPar.name(DefContants.X_ACCESS_TOKEN).description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
  84. pars.add(tokenPar.build());
  85. return pars;
  86. }
  87. /**
  88. * api文档的详细信息函数,注意这里的注解引用的是哪个
  89. *
  90. * @return
  91. */
  92. private ApiInfo apiInfo() {
  93. return new ApiInfoBuilder()
  94. // //大标题
  95. .title("Jeecg-Boot 后台服务API接口文档")
  96. // 版本号
  97. .version("1.0")
  98. // .termsOfServiceUrl("NO terms of service")
  99. // 描述
  100. .description("后台API接口")
  101. // 作者
  102. .contact("JEECG团队")
  103. .license("The Apache License, Version 2.0")
  104. .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
  105. .build();
  106. }
  107. }