Spring boot理財系統3 Swagger

swagger介紹

在Swagger模塊下添加依賴

創建Swagger的配置類

package com.qwl.manager.configuration;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("controller")
                .apiInfo(apiInfo())
                .select()
                .build();
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("HTTO API")
                .description("管理端接口")
                .termsOfServiceUrl("http://springfox.io")
                .contact("qwl")
                .license("Apache License Version 2.0")
                .version("2.0")
                .build();
    }
}

Swagger優化

1.選擇性顯示接口

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("controller")
                .apiInfo(apiInfo())
                .select()
                //通過包路徑進行篩選
                .apis(RequestHandlerSelectors.basePackage(ProductController.class.getPackage().getName()))
                //根據URL進行篩選
                .paths(PathSelectors.ant("/products/*"))
                .build();
    }

    @Bean
    public Docket defaultApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                //設置一個默認的API,實現分組
                .select().apis(RequestHandlerSelectors.basePackage(BasicErrorController.class.getPackage().getName()))
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("HTTO API")
                .description("管理端接口")
                .termsOfServiceUrl("http://springfox.io")
                .contact("qwl")
                .license("Apache License Version 2.0")
                .version("2.0")
                .build();
    }

}

2.詳細註釋說明

3.中文顯示

swagger模塊

  • @import、組合註解
  • 使用spring.factories
  • 使用@ConfigurationProperties

定義SwaggerInfo配置信息類

package com.qwl.swagger;

import org.springframework.stereotype.Component;

/**
 * swagger配置信息
 */
@Component
public class SwaggerInfo {
    private String groupName = "controller";
    private String basePackage;
    private String antPath;
    private String title = "HTTP API";
    private String description = "管理端接口";
    private String license = "Apache License Version 2.0";

    //set get
}

將manager模塊的SwaggerConfiguration類移到swagger中並修改

package com.qwl.swagger;

@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.qwl.swagger")
public class SwaggerConfiguration {

    @Autowired
    SwaggerInfo swaggerInfo;

    @Bean
    public Docket createRestApi(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName(swaggerInfo.getGroupName())
                .apiInfo(apiInfo());
        ApiSelectorBuilder builder =docket.select();

        if(!StringUtils.isEmpty(swaggerInfo.getBasePackage())){
            builder=builder.apis(RequestHandlerSelectors.basePackage(swaggerInfo.getBasePackage()));
        }

        if (!StringUtils.isEmpty(swaggerInfo.getAntPath())){
            builder = builder.paths(PathSelectors.ant(swaggerInfo.getAntPath()));
        }
        return builder.build();


    }

    @Bean
    public Docket defaultApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                //設置一個默認的API,實現分組
                .select().apis(RequestHandlerSelectors.basePackage(BasicErrorController.class.getPackage().getName()))
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title(swaggerInfo.getTitle())
                .description(swaggerInfo.getDescription())
                .termsOfServiceUrl("http://springfox.io")
                .contact("qwl")
                .license(swaggerInfo.getLicense())
                .version("2.0")
                .build();
    }

}

讓manager模塊使用swagger的配置

第一種導入

第二種使用註解

建立EnableSwagger註解

package com.qwl.swagger;

import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
 * 開啓swagger文檔自動生成功能
 */
@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = { java.lang.annotation.ElementType.TYPE })
@Documented
@Import(SwaggerConfiguration.class)
public @interface EnableMySwagger {

}

在manager中注入

組合註解

將SwaggerConfiguration上的@EnableSwagger2移到EnableSwagger上

第三種使用spring-factories

更新化配置

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章