zoukankan      html  css  js  c++  java
  • ARTS打卡计划第5周-REVIEW-SpringBoot的api版本化实践

    本周分享是:https://www.springboottutorial.com/spring-boot-versioning-for-rest-services 对restful接口进行版本化

    文中提到了4中版本化的方式,并且进行了优劣分析

    1、通过uri进行版本化

    • http://localhost:8080/v1/person
    • http://localhost:8080/v2/person

    2、通过url参数进行版本化

    • http://localhost:8080/person/param?version=1
    • http://localhost:8080/person/param?version=2
        @GetMapping(value = "/student/param", params = "version=1")
        public StudentV1 paramV1() {
          return new StudentV1("Bob Charlie");
        }
      
        @GetMapping(value = "/student/param", params = "version=2")
        public StudentV2 paramV2() {
          return new StudentV2(new Name("Bob", "Charlie"));
        }

     

    3、通过http的header添加版本信息

    • http://localhost:8080/person/header
      • headers=[X-API-VERSION=1]
    • http://localhost:8080/person/header
      • headers=[X-API-VERSION=2]
    @GetMapping(value = "/student/header", headers = "X-API-VERSION=1")
      public StudentV1 headerV1() {
        return new StudentV1("Bob Charlie");
      }
    
      @GetMapping(value = "/student/header", headers = "X-API-VERSION=2")
      public StudentV2 headerV2() {
        return new StudentV2(new Name("Bob", "Charlie"));
      }
    

      4、通过Media type进行版本化

    • http://localhost:8080/person/produces
      • headers[Accept=application/vnd.company.app-v1+json]
    • http://localhost:8080/person/produces

    headers[Accept=application/vnd.company.app-v2+json]

     @GetMapping(value = "/student/produces", produces = "application/vnd.company.app-v1+json")
      public StudentV1 producesV1() {
        return new StudentV1("Bob Charlie");
      }
    
      @GetMapping(value = "/student/produces", produces = "application/vnd.company.app-v2+json")
      public StudentV2 producesV2() {
        return new StudentV2(new Name("Bob", "Charlie"));
      }
    

      文中提到了几种技术选型所需要考虑的一些因素:

    1、uri污染,第一种和第二种会对uri进行污染

    2、错误使用http头,http头设计的时候不是用于版本化

    3、能否简单的在浏览器测试,主要是为了测试啊方便

    4、缓存,如果版本化消息放在http头,我们就无法根据url进行缓存,需要考虑header信息

    5、api文档化,文档生产的时候,如何标注同一个服务的不同版本

  • 相关阅读:
    SASS常用方法
    Vue 全局过滤和局部过滤
    Java进阶知识点8:高可扩展架构的利器
    InnoDB MVCC RR隔离级别下的数据可见性总结
    记一次诡异的网络故障排除
    数据库的读锁和写锁在业务上的应用场景总结
    Gradle配置IDEA正常识别JPA Metamodel Generator动态生成的代码
    程序员容易读错的单词
    Java进阶知识点7:不要只会写synchronized
    Java进阶知识点6:并发容器背后的设计理念
  • 原文地址:https://www.cnblogs.com/dongqiSilent/p/10882066.html
Copyright © 2011-2022 走看看