通常来说,API的版本管理有2种方案:
以v1或v2这种版本号为前缀:
请求:http://localhost:8080/v1/student 响应:{"name":"aa bb"} 请求:http://localhost:8080/v2/student 响应:{"name":{"firstName":"aa","lastName":"bb"}}
或者将版本以参数形式拼接到后面:
请求:http://localhost:8080/student/param?version=v1 响应:{"name":"aa bb"} 请求:http://localhost:8080/student/param?version=v2 响应:{"name":{"firstName":"aa","lastName":"bb"}}
优点:
缺点:
例:
请求:http://localhost:8080/student/header header:X-API-VERSION = 1 请求:http://localhost:8080/student/header header:X-API-VERSION = 2
优点:
缺点:
通常来说,URI的方式更符合我们的直觉。
以nestjs工程为例:
@ApiVersion('v1')
,这样在ApiVersion这个装饰器中不管是变更URI还是自定义Header,都与每个Controller的方法的业务代码无关。如果是Deno的oak_nest工程,请更新到v1.10.2版本,Controller、Get/Post等装饰器都添加了可选参数alias和isAbsolute,比如:
@Controller("user", {
alias: "/v1/user/",
})
export class UserController {
@Get("/info")
info(context: Context) {
context.response.body = "info";
}
}
或者:
@Controller("/user")
export class UserController {
@Get("/info", {
alias: "/v1/user/info",
})
info() {}
}
阅读量:2005
点赞量:0
收藏量:0