侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 107 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

spring boot swagger2 接口多版本控制踩坑记录

管理员
2020-09-27 / 0 评论 / 0 点赞 / 4 阅读 / 12685 字

引入包:

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--解决进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本-->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>
        <!-- swagger2 增强UI ,拥有好看的界面, 和接口分组,排序等功能,如不引用可自行删除-->
        <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
        <!-- https://doc.xiaominfo.com/-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

 

swagger配置并启用:
 springfox.documentation.service.* java.util.*"dev"  SwaggerConfig  Logger log = LoggerFactory.getLogger(
     "deprecation"  ApiInfoBuilder().title("接口文档""服务端通用接口").version("1.0"
     
     List<ApiKey><ApiKey> apiKeyList =  ArrayList<ApiKey> ApiKey("Authorization", "Authorization", "header" List<SecurityContext><SecurityContext> securityContexts =  ArrayList<>"^(?!auth).*$" List<SecurityReference>=  AuthorizationScope("global", "accessEverything"=  AuthorizationScope[10] =<SecurityReference> securityReferences =  ArrayList<> SecurityReference("Authorization"
     
     List<ResponseMessage><ResponseMessage> responseMessageList =  ArrayList<>
        responseMessageList.add( ResponseMessageBuilder().code(200).message("响应成功" ResponseMessageBuilder().code(500).message("服务器内部错误"
     List<Map<String, Object>>
            Class<Enum> clazz = (Class<Enum><Map<String, Object>> list =  ArrayList<Map<String, Object>>
            Enum[] enumConstants =
            Method getCode = clazz.getMethod("getCode"= clazz.getMethod("getMessage"<String, Object> map = =  HashMap<String, Object>
                map.put("code""message"  ->
                     (method.isAnnotatedWith(ApiVersion.= method.getHandlerMethod().getMethodAnnotation(ApiVersion. (apiVersion.value() !=  
                    ApiVersion annotationOnClass = method.getHandlerMethod().getBeanType().getAnnotation(ApiVersion. (annotationOnClass !=  (annotationOnClass.value() !=
          if (method.groupName().equals("token-controller")) return true;
            return false;
                })
                .paths(PathSelectors.any())
                .build().securitySchemes(securitySchemes()).securityContexts(securityContexts());
  afterPropertiesSet() 
        Class<CustomVersion> clazz = CustomVersion.=
        AutowireCapableBeanFactory autowireCapableBeanFactory = (autowireCapableBeanFactory =
                AbstractBeanDefinition beanDefinition ="buildDocket", "swaggerConfig"

自定义注解实API版本控制:

ApiVersion.class
/**
 * API版本控制注解
 * Created on 2019/4/18 11:17.
 *
 * @author caogu */@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mappingpublic @interface ApiVersion {//    //标识版本号//    int value() default 1;
    /**
     * 分组名(即版本号),可以传入多个
     *
     * @return
     */
    String value() default "v1";
}

 

自定义一个API版本号控制类

/**
 * 版本管理接口 */public interface CustomVersion {
    String VERSION_1 = "v1";
    String VERSION_2 = "v2";
}

 补个美美的效果图

 

 



 


0

评论区