In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
本篇内容主要讲解"RESTful API怎么进行版本控制",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"RESTful API怎么进行版本控制"吧!
您将学到
为什么我们需要对RESTful API 进行版本控制?可用的版本控制有哪些?如何实现基于 Restful 的版本控制?为什么我们需要对RESTful API进行版本化
最好的版本控制方法是不进行版本控制。只要不需要版本控制,就不要版本控制。
构建向后兼容的服务,以便尽可能避免版本控制!
然而,在许多情况下我们都需要进行版本控制,然我们看看下面具体的例子:
最初,你有个这个版本的Student服务,返回数据如下:
{
"name": "Bob Charlie"
}
后来,您希望将学生的名字拆分,因此创建了这个版本的服务。
{
"name": {
"firstName": "Bob",
"lastName": "Charlie"
}
}
您可以从同一个服务支持这两个请求,但是随着每个版本的需求多样化,它会变得越来越复杂。
在这种情况下,版本控制就成必不可少,强制性的了。
接下来让我们创建一个简单的SpringBoot的maven项目,并理解对 RESTful 服务进行版本控制的4种不同方法。
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
test
几个用于实现版本控制的Bean
第一个版本的 Bean
@Data
@AllArgsConstructor
public class StudentV1 {
private String name;
}
第二个版本的 Bean
@Data
public class StudentV2 {
private Name name;
}
StudentV2使用的Name实体
@Data
@AllArgsConstructor
public class Name {
private String firstName;
private String lastName;
} Restful 版本控制的方法
我们希望创建两个版本的服务,一个返回 StudentV1,另一个返回 StudentV2。
让我们来看看创建相同服务版本的4种不同方法。
通过 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/student
响应:{"name":"javadaily"}
请求:http://localhost:8080/v2/student
响应:{"name":{"firstName":"javadaily","lastName":"JAVA日知录"}}
通过请求参数进行版本控制
版本控制的第二种方法是使用请求参数来区分版本。请求示例如下所示:
http://localhost:8080/student/param?version=1http://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=1
响应:{"name":"javadaily"}
请求:http://localhost:8080/student/param?version=2
响应:{"name":{"firstName":"javadaily","lastName":"JAVA日知录"}}
通过自定义Header进行版本控制
版本控制的第三种方法是使用请求头来区分版本,请求示例如下:
http://localhost:8080/student/header
headers=[X-API-VERSION=1]
http://localhost:8080/student/header
headers=[X-API-VERSION=2]
实现方式如下所示:
@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日知录"));
}
}
下图展示了我们如何使用Postman执行带有请求头的Get请求方法。
请求:http://localhost:8080/student/header
header:X-API-VERSION = 1
请求:http://localhost:8080/student/header
header:X-API-VERSION = 2
Version control by media type
The last version control method is to use an Accept Header in a request. An example request is as follows:
http://localhost:8080/student/produce
headers=[Accept=application/api-v1+json]
http://localhost:8080/student/produce
headers=[Accept=application/api-v2+json]
This can be achieved as follows:
@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日明RECORD "));
}
}
The following figure shows how we use Postman to execute the Get method with the request Accept.
Request: http://localhost:8080/student/produce
header:Accept = application/api-v1+json
Request: http://localhost:8080/student/produce
header:Accept = application/api-v2+json
Factors Affecting Version Selection
The following factors influence the choice of version control
URI Pollution- URL version and request parameter versioning pollute the URI space. Abuse Request Header-The Accept request header is not designed for version control. Caching-If you use header-based version control, we can't cache based on URLs alone, you need to consider specific request headers. Can it be executed directly in the browser? - If you have non-technical consumers, URL-based versions will be easier to use because they can be executed directly on the browser. API Documentation-How do I get documentation to understand that two different URLs are versions of the same service? In fact, there is no perfect version control solution, you need to choose according to the actual situation of the project. At this point, I believe that everyone has a deeper understanding of "how to version control RESTful API". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.