swaggerUI接口版本管理

首先定义一个注解

	@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiVersion {
 
    /**
     * 接口版本号(对应swagger中的group)
     * @return String[]
     */
    String[] group();
 
}

然后再定义一个管理版本号的接口

public interface ApiVersionConstant {
    String FAP_APP100 = "app1.0.0";
    String FAP_APP101 = "app1.0.1";
    String FAP_APP102 = "app1.0.2";
}

再在swagger配置类中添加Docket方法

@Configuration
public class Swagger2Config {
 
	//默认版本的接口api-docs分组
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				//指定要扫描的包的路径
				.apis(RequestHandlerSelectors.basePackage("com.vhukze.controller"))
				.paths(PathSelectors.any())
				.build();
	}
	
	//app1.0.0版本对外接口
    @Bean
    public Docket vApp100(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(ApiVersionConstant.FAP_APP100)
                .select()
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    if(apiVersion!=null&&Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.FAP_APP100)){
                        return true;
                    }
                    return false;
                })//controller路径
                .paths(PathSelectors.any())
                .build();
    }
    //app1.0.1版本对外接口
    @Bean
    public Docket vApp101(){
    	return new Docket(DocumentationType.SWAGGER_2)
    			.apiInfo(apiInfo())
    			.groupName(ApiVersionConstant.FAP_APP101)
    			.select()
    			.apis(input -> {
    				ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
    				if(apiVersion!=null&&Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.FAP_APP101)){
    					return true;
    				}
    				return false;
    			})//controller路径
    			.paths(PathSelectors.any())
    			.build();
    }
 
	//构建api文档的详细信息函数
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				//页面标题
				.title("测试使用Swagger")
				//创建人
				.contact(new Contact("DHEE", "www.baidu.com", "vhukze@qq.com"))
				//版本号
				.version("1.1")
				//描述
				.description("管理系统")
				
				.build();
	}
}
# 接下来在接口上使用这个注解
@ApiOperation(value = "新增用户", notes = "输入用户名密码,进行注册", httpMethod = "POST")
	@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "UserDemo")
	@ApiResponses({
	    //code重复的情况下,第一个声明的生效。
	    @ApiResponse(code = 200,message = "插入成功" ),
	})
	@ApiVersion(group = ApiVersionConstant.FAP_APP101)
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public Boolean insertUser(@RequestBody UserDemo user) {
		boolean flag = userServer.insertUser(user);
		return flag;
	}
	
	
 
	@ApiOperation(value = "删除用户", notes = "根据用户名删除指定用户", httpMethod = "DELETE")
	@ApiImplicitParam(name = "username", value = "用户的用户名", required = true, dataType = "String")
	@ApiImplicitParams({
		@ApiImplicitParam(
			name = "username", value = "用户的用户名", required = true, dataType = "String"
		)
	})
	@ApiResponses({
	    //code重复的情况下,第一个声明的生效。
	    @ApiResponse(code = 200,message = "删除成功" ),
	    @ApiResponse(code = 202,message = "删除失败,用户不存在")
	})
	@ApiVersion(group = ApiVersionConstant.FAP_APP100)
	@RequestMapping(value = "delete", method = RequestMethod.DELETE)
	public Boolean deleteUser(String username,HttpServletResponse response) {
		Boolean isSuccess = userServer.deleteUser(username);
		if(!isSuccess) {
			response.setStatus(202);
		}
		return isSuccess;
		

这样在ui界面就可以选择不同版本号的接口,或者选择default全部接口

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值