作者:微信小助手
发布时间:2021-12-17T08:31:53
大家好,我是不才陈某~ 在实际项目开发中我们经常需要对接口进行版本管理。那今天我们就来聊聊为什么需要版本控制,以及如何对REST API进行版本控制。我们将讨论4种版本控制的方法,并比较不同的方法。 通过此文您将学到 最好的版本控制方法是不进行版本控制。只要不需要版本控制,就不要版本控制。 构建向后兼容的服务,以便尽可能避免版本控制! 然而,在许多情况下我们都需要进行版本控制,然我们看看下面具体的例子: 最初,你有个这个版本的Student服务,返回数据如下: 后来,您希望将学生的名字拆分,因此创建了这个版本的服务。 您可以从同一个服务支持这两个请求,但是随着每个版本的需求多样化,它会变得越来越复杂。 在这种情况下,版本控制就成必不可少,强制性的了。 接下来让我们创建一个简单的SpringBoot的maven项目,并理解对 RESTful 服务进行版本控制的4种不同方法。 第一个版本的 Bean 第二个版本的 Bean StudentV2使用的Name实体 我们希望创建两个版本的服务,一个返回 StudentV1,另一个返回 StudentV2。 让我们来看看创建相同服务版本的4种不同方法。 请求: 响应:{"name":"javadaily"} 请求: 响应:{"name":{"firstName":"javadaily","lastName":"JAVA日知录"}} 版本控制的第二种方法是使用请求参数来区分版本。请求示例如下所示: 实现方式如下: 请求: 响应:{"name":"javadaily"} 请求: 响应:{"name":{"firstName":"javadaily","lastName":"JAVA日知录"}} 版本控制的第三种方法是使用请求头来区分版本,请求示例如下: 实现方式如下所示: 下图展示了我们如何使用Postman执行带有请求头的Get请求方法。 请求: 请求: 最后一种版本控制方法是在请求中使用Accept Header,请求示例如下: 实现方式如下: 下图展示了我们如何使用Postman执行带有请求Accept的Get方法。 请求: 请求: 以下因素影响版本控制的选择 事实上,并没有完美的版本控制解决方案,你需要根据项目实际情况进行选择。 下面列表展示了主要API提供商使用的不同版本控制方法: 媒体类型的版本控制 自定义Header URI路径 请求参数控制 好了,今天的文章就到这里了,希望能对你有所帮助。
  
为什么我们需要对RESTful API进行版本化
 
  “
  
 {
  "name": "Bob Charlie"
}{
  "name": {
    "firstName": "Bob",
    "lastName": "Charlie"
  }
}<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>几个用于实现版本控制的Bean
 @Data
@AllArgsConstructor
public class StudentV1 {
    private String name;
}@Data
public class StudentV2 {
    private Name name;
}@Data
@AllArgsConstructor
public class Name {
    private String firstName;
    private String lastName;
}Restful 版本控制的方法
 通过 URI 进行版本控制
 @RestController
public class StudentUriController {
    @GetMapping("v1/student")
    public StudentV1 studentV1() {
        return new StudentV1("javadaily");
    }
    @GetMapping("v2/student")
    public StudentV2 studentV2() {
        return new StudentV2(new Name("javadaily", "JAVA日知录"));
    }
}http://localhost:8080/v1/studenthttp://localhost:8080/v2/student通过请求参数进行版本控制
 
  
http://localhost:8080/student/param?version=1
   http://localhost:8080/student/param?version=2
   @RestController
public class StudentParmController {
    @GetMapping(value="/student/param",params = "version=1")
    public StudentV1 studentV1() {
        return new StudentV1("javadaily");
    }
    @GetMapping(value="/student/param",params = "version=2")
    public StudentV2 studentV2() {
        return new StudentV2(new Name("javadaily", "JAVA日知录"));
    }
}http://localhost:8080/student/param?version=1http://localhost:8080/student/param?version=2通过自定义Header进行版本控制
 
  
http://localhost:8080/student/header
   
http://localhost:8080/student/header
   
@RestController
public class StudentHeaderController {
    @GetMapping(value="/student/header",headers = "X-API-VERSION=1")
    public StudentV1 studentV1() {
        return new StudentV1("javadaily");
    }
    @GetMapping(value="/student/header",headers = "X-API-VERSION=2")
    public StudentV2 studentV2() {
        return new StudentV2(new Name("javadaily", "JAVA日知录"));
    }
}http://localhost:8080/student/header 
header:X-API-VERSION = 1 
  
  http://localhost:8080/student/header 
header:X-API-VERSION = 2 
  
  通过媒体类型进行版本控制
 
  
http://localhost:8080/student/produce
   
headers=[Accept=application/api-v1+json]
    http://localhost:8080/student/produce
   
headers=[Accept=application/api-v2+json]
    @RestController
public class StudentProduceController {
    @GetMapping(value="/student/produce",produces = "application/api-v1+json")
    public StudentV1 studentV1() {
        return new StudentV1("javadaily");
    }
    @GetMapping(value="/student/produce",produces = "application/api-v2+json")
    public StudentV2 studentV2() {
        return new StudentV2(new Name("javadaily", "JAVA日知录"));
    }
}http://localhost:8080/student/produce 
header:Accept = application/api-v1+json 
  
  http://localhost:8080/student/produce 
header:Accept = application/api-v2+json 
  
  影响版本选择的因素
 
  
  “
  
 
  
   
   
   
   
求点赞、在看、分享三连