创建自己的Swagger Starter

步骤

  1. 新建maven工程,引入依赖

     2.7.0
    
    
     
         org.springframework.boot
         spring-boot-autoconfigure
         RELEASE
     
    
     
         org.springframework.boot
         spring-boot-configuration-processor
         RELEASE
         true
     
     
         io.springfox
         springfox-swagger-ui
         ${version.swagger}
     
     
         io.springfox
         springfox-swagger2
         ${version.swagger}
     
    
  2. 创建说明配置类,省略get set方法...

     @ConfigurationProperties(prefix="swagger")
     public class SwaggerProperties {
     
         /**是否开启swagger**/
         private Boolean enabled;
     
         private String basePackage = "";
     
         private String title  = "Spring Boot中使用Swagger2构建RESTful APIs";
     
         private String description = "更多Spring Boot相关文章请关注:https://jetbrains.org.cn/";
     
         private String termsOfServiceUrl = "https://jetbrains.org.cn/" ;
     
         private String contact ="Mr.Chang";
     
         private String version = "1.0";
    
  3. 创建swagger配置

             @Configuration
             @ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
             @Import({
                     Swagger2DocumentationConfiguration.class})
             public class Swagger2Configuration {
             
             }
    
  4. 创建自动配置类

     @Configuration
     @Import({
             Swagger2Configuration.class
     })
     
     public class SwaggerAutoConfiguration  {
         @Bean
         @ConditionalOnMissingBean
         public SwaggerProperties swaggerProperties() {
             return new SwaggerProperties();
         }
     
     
         @Bean
         @ConditionalOnMissingBean
         @ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
         public Docket createRestApi(SwaggerProperties swaggerProperties) {
     
             return new Docket(DocumentationType.SWAGGER_2)
                     .apiInfo(new ApiInfoBuilder()
                             .title(swaggerProperties.getTitle())
                             .description(swaggerProperties.getDescription())
                             .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                             .contact(swaggerProperties.getContact())
                             .version(swaggerProperties.getVersion())
                             .build())
                     .select()
                     .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                     .paths(PathSelectors.any())
                     .build();
         }
     }
    
  5. 创建启动注解

     @Target({ElementType.TYPE})
     @Retention(RetentionPolicy.RUNTIME)
     @Documented
     @Inherited
     @Import({SwaggerAutoConfiguration.class})
     public @interface EnableSwagger2 {
     }
    
  6. 使用

    • 第一次使用,将项目打包上传至私服,其他项目直接饮用,在springboot启动类上书写@EnableSwagger2即可
    • 非第一次使用。其他项目直接饮用,在springboot启动类上书写@EnableSwagger2即可
创建自己的Swagger Starter_第1张图片
image

你可能感兴趣的:(创建自己的Swagger Starter)