首先定义一个注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ApiVersion { /** * 接口版本号(对应swagger中的group) * @return String[] */ String[] group(); }
然后再定义一个管理版本号的接口
1.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全部接口