介绍

  • 产品的迭代有时候会遇到接口需要版本,于是就有了这个需求
  • 微侵入使用
  • 参考与感谢

    引入依赖

    1. <dependency>
    2. <groupId>cn.jdevelops</groupId>
    3. <artifactId>aops-apiversion</artifactId>
    4. <version>2.0.2</version>
    5. </dependency>

    使用

    • 注解形式无需额外配置
    • 默认返回1.0的接口
    • 参数名:version


版本 跟在 header中

  1. package io.tn.aop.version.controller;
  2. import com.detabes.version.annotation.ApiVersion;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * 版本
  7. *
  8. * @author tn
  9. * @version 1
  10. * @date 2021/1/30 21:53
  11. */
  12. @RestController
  13. public class VersionController {
  14. /********* header *******/
  15. @GetMapping("/api/test_2")
  16. @ApiVersion(1.0)
  17. public String version1_2() {
  18. return "Hello,Welcome to version 1.0 header";
  19. }
  20. @GetMapping("/api/test_2")
  21. @ApiVersion(2.0)
  22. public String version2_2() {
  23. return "Hello,Welcome to version 2.0 header";
  24. }
  25. @GetMapping("/api/test_2")
  26. @ApiVersion(3.0)
  27. public String version3_2() {
  28. return "Hello,Welcome to version 3.0 header";
  29. }
  30. /********* parameter *******/
  31. }

版本 跟在 参数中

  1. package io.tn.aop.version.controller;
  2. import com.detabes.version.annotation.ApiVersion;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * 版本
  7. *
  8. * @author tn
  9. * @version 1
  10. * @date 2021/1/30 21:53
  11. */
  12. @RestController
  13. public class VersionController {
  14. /********* parameter *******/
  15. @GetMapping("/api/test_1")
  16. @ApiVersion(1.0)
  17. public String version1_1(String version) {
  18. return "Hello,Welcome to version 1.0 parameter";
  19. }
  20. @GetMapping("/api/test_1")
  21. @ApiVersion(2.0)
  22. public String version2_1(String version) {
  23. return "Hello,Welcome to version 2.0 parameter";
  24. }
  25. @GetMapping("/api/test_1")
  26. @ApiVersion(3.0)
  27. public String version3_1(String version) {
  28. return "Hello,Welcome to version 3.0 parameter";
  29. }
  30. /********* parameter *******/
  31. }

版本 跟在 url 中

这种方式我给删除了。觉得验证的不是很安全


示例项目地址

https://github.com/en-o/Jdevelops-Example/tree/main/ApiVersion