swagger

Posted cxyyh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swagger 相关的知识,希望对你有一定的参考价值。

创建创建microservicecloud-swagger

pom文件

eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
      ###是否向注册中心注册自己
    register-with-eureka: true
      ###是否需要从eureka上获取注册信息
    fetch-registry: true

 yml文件

eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
      ###是否向注册中心注册自己
    register-with-eureka: true
      ###是否需要从eureka上获取注册信息
    fetch-registry: true

 

 properties文件

#swagger配置
swagger.enabled=true
swagger.title=Auth-Server-API
swagger.description=Auth-Server接口说明
swagger.basePackage=com.yehui.controller
swagger.basePath=/**
swagger.excludePath=/error
swagger.version=1.0
#接口管理员配置
swagger.contact.name=yehui
[email protected]
[email protected]
server.port=9003
eureka.client.u

 

 扫描单包  swagger类

@Configuration
@EnableSwagger2 //swagger注解
public class SwaggerConfig {

    @Autowired
    private Environment ev;

    //得到扫描包
    public String getBasePack(){
        String basePage = ev.getProperty("swagger.basePackage");
        return isEmpty(basePage)?"":basePage;
    }

    //得到标题
    public String getTitle(){
        String title = ev.getProperty("swagger.title");
        return isEmpty(title)?"":title;
    }
    //接口文档说明
    public String getDescription(){
        String description = ev.getProperty("swagger.description");
        return isEmpty(description)?"":description;
    }
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                // api扫包
                .apis(RequestHandlerSelectors.basePackage(getBasePack())).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(getTitle()).description(getDescription())
                .termsOfServiceUrl("http://www.itmayiedu.com")//官方网址
                 .contact(contact())
                .version(getVersion()).build();
    }
    //获取版本号
    public String getVersion(){
        String version = ev.getProperty("swagger.version");
        return isEmpty(version)?"":version;
    }
    //接口管理员配置
    public Contact contact(){
        String name = ev.getProperty("swagger.contact.name");
        String url = ev.getProperty("swagger.contact.url");
        String email = ev.getProperty("swagger.contact.email");
        name = isEmpty(name)?"":name;
        url = isEmpty(url)?"":url;
        email = isEmpty(email)?"":email;
        return new Contact(name,url,email);
    }
    /**
     * 判断字符串是否为空,null或者length=0表示空值
     *
     * @param str
     * @return true-空字符串,false-非空字符串
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

}

 

支持多包的swagger类

@Component
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    private Environment env;

    private String basePackage() { // controller接口所在的包
        String basePackage = env.getProperty("swagger.basePackage");
        return CommUtil.isEmpty(basePackage) ? "" : basePackage;
    }

    private String title() { // 当前文档的标题
        String title = env.getProperty("swagger.title");
        return CommUtil.isEmpty(title) ? "说明文档" : title;
    }

    private String description() { // 当前文档的详细描述
        String description = env.getProperty("swagger.description");
        return CommUtil.isEmpty(description) ? "接口说明文档" : description;
    }

    private String version() { // 当前文档的版本
        String version = env.getProperty("swagger.version");
        return CommUtil.isEmpty(version) ? "1.0" : version;
    }

    private Contact contact() { // 接口管理员配置
        String name = env.getProperty("swagger.contact.name");
        String url = env.getProperty("swagger.contact.url");
        String email = env.getProperty("swagger.contact.email");

        return new Contact(CommUtil.isEmpty(name) ? "admin" : name, CommUtil.isEmpty(url) ? "[email protected]" : url,
                CommUtil.isEmpty(email) ? "[email protected]" : email);
    }

    @Bean
    public Docket newDocket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(input -> declaringClass(input).transform(handlerPackage(basePackage())).or(true))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title()).description(description()).version(version()).contact(contact())
                .build();
    }

    private Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(",")) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}

 

启动服务测试

 

以上是关于swagger 的主要内容,如果未能解决你的问题,请参考以下文章

用swagger生成接口文档代码

swagger文档转换为WebApiClient声明式代码

swagger-doc 让你high起来的swagger

swagger-doc 让你high起来的swagger

swagger-doc 让你high起来的swagger

Swagger(狂神说Java)(内含教学视频+源代码)