swagger弹性拓展

目标

初级版swagger配置
在swagger好用的基础上,变得更加好用

  1. 将swagger一些基础信息改为属性注入
  2. 增加了API是否显示的逻辑

Swagger配置类

package com.springboot.demo.properties;

import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author zihui
 * @version 1.0
 * @title: SwaggerProperties
 * @description: TODO
 * @date 2021-01-14 10:39:21
 */
@Data
public class SwaggerProperties {

    /**
     * swagger会解析的包路径
     **/
    private String basePackage = "com.springboot.demo";
    /**
     * swagger会解析的url规则
     **/
    private List<String> basePath = Lists.newArrayList();
    /**
     * 在basePath基础上需要排除的url规则
     **/
    private List<String> excludePath = Lists.newArrayList();

    /**
     * 标题
     **/
    private String title = "API接口文档";
    /**
     * 描述
     **/
    private String description = "API接口文档,及相关接口的说明";
    /**
     * 版本
     **/
    private String version = "1.0.0";
    /**
     * 许可证
     **/
    private String license = "";
    /**
     * 许可证URL
     **/
    private String licenseUrl = "";
    /**
     * 服务条款URL
     **/
    private String termsOfServiceUrl = "";

    /**
     * host信息
     **/
    private String host = "";
    /**
     * 联系人信息
     */
    private Contact contact = new Contact();

    @Data
    @NoArgsConstructor
    public static class Contact {

        /**
         * 联系人
         **/
        private String name = "zihui";
        /**
         * 联系人url
         **/
        private String url = "";
        /**
         * 联系人email
         **/
        private String email = "";

    }

}

系统配置类

package com.springboot.demo.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author zihui
 * @version 1.0
 * @title: RootProperties
 * @description: TODO
 * @date 2021-01-14 10:37:21
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "springboot.demo")
public class RootProperties {
    /**
     * swagger配置
     */
    private SwaggerProperties swagger=new SwaggerProperties();
}

Spring Boot Configuration Annotation Processor not found in classpath这个类会报这个错,根据提示信息,查询此注解的使用关于怎么指定classpath,进而查询location,Spring Boot1.5以上版本@ConfigurationProperties取消location注解,反正就是在1.5版本后改变了@ConfigurationProperties注解的使用。
打开idea提示连接https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor,看下到底是什么回事,发现直接给了解决方法,那就是在pom.xml文件中加入依赖spring-boot-configuration-processor

pom.xml

        <!--配置文件处理器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

现在来看RootProperties.class换了个报错:Not registered via @EnableConfigurationProperties or marked as Spring component
翻译:没有通过@EnableConfigurationProperties这个注解进行注册或者没有标记为Spring的组建
Not registered via @EnableConfigurationProperties or marked as Spring component

启动类

解决方案如下:在启动类上通过@EnableConfigurationProperties这个注解将该类注册为属性配置的类
在这里插入图片描述

优化后Swagger配置类

package com.springboot.demo.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import com.springboot.demo.properties.RootProperties;
import com.springboot.demo.properties.SwaggerProperties;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author zihui
 * @version 1.0
 * @title: SwaggerConfiguration
 * @description: TODO
 * @date 2021-01-14 11:29:49
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    private static final String DEFAULT_EXCLUDE_PATH = "/error";
    private static final String BASE_PATH = "/**";

    @Autowired
    private RootProperties properties;

    /**
     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     * 在构建文档的时候过滤哪些API接口
     * @return
     */
    @Bean //作为bean纳入spring容器
    public Docket api() {
        SwaggerProperties swaggerProperties = properties.getSwagger();
        // base-path处理
        if (CollectionUtils.isEmpty(swaggerProperties.getBasePath())) {
            swaggerProperties.getBasePath().add(BASE_PATH);
        }
        List<Predicate<String>> basePath = Lists.newArrayList();
        swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));

        // exclude-path处理
        if (CollectionUtils.isEmpty(swaggerProperties.getExcludePath())) {
            swaggerProperties.getExcludePath().add(DEFAULT_EXCLUDE_PATH);
        }
        List<Predicate<String>> excludePath = Lists.newArrayList();
        swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path)));
        return new Docket(DocumentationType.SWAGGER_2)
                .host(swaggerProperties.getHost())
                .apiInfo(apiInfo(swaggerProperties))
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))// 只显示添加了@Api注解的类
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))//  只显示指定包下的API
                .paths(PathSelectors.any())//   所有路径下都可以扫描
                .paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath)))//哪些路径需要过滤,哪些路径可以扫描
                .build();
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     * @return
     */
    private ApiInfo apiInfo(SwaggerProperties swaggerProperties){
        return  new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .license(swaggerProperties.getLicense())
                .licenseUrl(swaggerProperties.getLicenseUrl())
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                .contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
                .version(swaggerProperties.getVersion())
                .build();
    }
}

效果

只显示我们需要的api
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值