{"id":37020830,"url":"https://github.com/pdkst/request-version","last_synced_at":"2026-01-14T02:26:07.202Z","repository":{"id":57734459,"uuid":"479756529","full_name":"pdkst/request-version","owner":"pdkst","description":"基于spring boot的接口请求版本控制，分发请求到指定的版本","archived":false,"fork":false,"pushed_at":"2022-08-23T15:38:00.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-12T21:47:35.024Z","etag":null,"topics":["java","spring"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pdkst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-09T14:50:59.000Z","updated_at":"2022-05-20T08:24:30.000Z","dependencies_parsed_at":"2022-08-24T01:11:12.605Z","dependency_job_id":null,"html_url":"https://github.com/pdkst/request-version","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pdkst/request-version","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdkst%2Frequest-version","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdkst%2Frequest-version/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdkst%2Frequest-version/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdkst%2Frequest-version/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pdkst","download_url":"https://codeload.github.com/pdkst/request-version/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdkst%2Frequest-version/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","spring"],"created_at":"2026-01-14T02:26:06.393Z","updated_at":"2026-01-14T02:26:07.191Z","avatar_url":"https://github.com/pdkst.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# request-version\n\n基于请求的版本号来分发接口到方法上， 如果只有一个接口，则会忽略此项设置\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.pdkst/request-version/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.pdkst/request-version)\n\n# 使用场景\n\n- 旧接口无法变动，新接口需要做破坏性更新\n- 不同版本返回不一样数据\n- app兼容旧版本\n\n# 快速开始\n\n### maven引入\n\n```xml\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.pdkst\u003c/groupId\u003e\n    \u003cartifactId\u003erequest-version\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### 1.没有自定义`RequestMappingHandlerMapping`的情况下\n\n1. 引入jar\n2. 复制一样的接口\n3. 标注版本号 `@SinceVersion`\n4. 请求增加版本号字段（默认`x-version`)\n\n### 2.已使用自定义`RequestMappingHandlerMapping`的场景\n\n1. 引入jar\n2. 在自定义的`RequestMappingHandlerMapping`中重写相关接口：\n    - getCustomMethodCondition\n    - getCustomTypeCondition\n3. 其他步骤同1\n\n# 如何自定义使用\n\n假设接口是： `GET http://localhost:8080/version`\n\n### 1. 从请求头中获取版本：\n\n```java\n @Bean\npublic VersionRequestConditionProvider versionRequestConditionProvider(){\n        return new HeaderVersionRequestConditionProvider(\"x-version\");\n        }\n```\n\n测试：\n\n```http request\nGET http://localhost:8080/version\nx-version: 1.0\n```\n\n### 2.从UserAgent中获取版本\n\n```java\n@Bean\npublic VersionRequestConditionProvider versionRequestConditionProvider(){\n        return new UserAgentVersionRequestConditionProvider(\"version\");\n        }\n```\n\n测试：\n\n```http request\nGET http://localhost:8080/version\nuser-agent: version/1.0\n```\n\n### 3.从FormValue中获取版本\n\n```java\n@Bean\npublic VersionRequestConditionProvider versionRequestConditionProvider(){\n        return new FormVersionRequestConditionProvider(\"v\");\n        }\n```\n\n测试：\n\n```http request\nGET http://localhost:8080/version?v=1.0\n```\n\n# 版本原理\n\n1. 利用spring自定义`RequestCondition`的预留接口，实现同样的请求按照接口不同分发给不同方法，此方法比spring匹配优先级更低\n    - PatternsRequestCondition 路径\n    - RequestMethodsRequestCondition 方法\n    - ParamsRequestCondition 参数\n    - HeadersRequestCondition 请求头\n    - ConsumesRequestCondition Consumes\n    - ProducesRequestCondition Produces\n    - customCondition **自定义**←\n2. 版本管理逻辑同自然版本逻辑，`2.0.0`\u003e`1.9.9`\n3. 多个版本都会返回，但是按照匹配的版本，分发给版本最高的那个\n4. 如果请求不存在版本，则默认分发给版本最大的那个（即最新的）\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdkst%2Frequest-version","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdkst%2Frequest-version","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdkst%2Frequest-version/lists"}