介绍
- 产品的迭代有时候会遇到接口需要版本,于是就有了这个需求
- 微侵入使用
- 参考与感谢
引入依赖
<dependency>
<groupId>cn.jdevelops</groupId>
<artifactId>aops-apiversion</artifactId>
<version>2.0.2</version>
</dependency>
使用
- 注解形式无需额外配置
- 默认返回1.0的接口
- 参数名:version
版本 跟在 header中
package io.tn.aop.version.controller;
import com.detabes.version.annotation.ApiVersion;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 版本
*
* @author tn
* @version 1
* @date 2021/1/30 21:53
*/
@RestController
public class VersionController {
/********* header *******/
@GetMapping("/api/test_2")
@ApiVersion(1.0)
public String version1_2() {
return "Hello,Welcome to version 1.0 header";
}
@GetMapping("/api/test_2")
@ApiVersion(2.0)
public String version2_2() {
return "Hello,Welcome to version 2.0 header";
}
@GetMapping("/api/test_2")
@ApiVersion(3.0)
public String version3_2() {
return "Hello,Welcome to version 3.0 header";
}
/********* parameter *******/
}
版本 跟在 参数中
package io.tn.aop.version.controller;
import com.detabes.version.annotation.ApiVersion;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 版本
*
* @author tn
* @version 1
* @date 2021/1/30 21:53
*/
@RestController
public class VersionController {
/********* parameter *******/
@GetMapping("/api/test_1")
@ApiVersion(1.0)
public String version1_1(String version) {
return "Hello,Welcome to version 1.0 parameter";
}
@GetMapping("/api/test_1")
@ApiVersion(2.0)
public String version2_1(String version) {
return "Hello,Welcome to version 2.0 parameter";
}
@GetMapping("/api/test_1")
@ApiVersion(3.0)
public String version3_1(String version) {
return "Hello,Welcome to version 3.0 parameter";
}
/********* parameter *******/
}
版本 跟在 url 中
这种方式我给删除了。觉得验证的不是很安全
示例项目地址
https://github.com/en-o/Jdevelops-Example/tree/main/ApiVersion