expression
REST: serve two API versions
A public version is a contract, not the build number of the server. Keep an existing representation available while introducing an incompatible representation on another version. Both routes can use the same domain service. Path versions are a design choice, not a universal HTTP rule. Standalone synthetic example. Expected stdout (not executed): /v1/items/7 name /v2/items/7 display_name Common mistake: Renaming a field in a supported version without a migration plan.
Formal Syntax & Structure
rust syntaxrust
GET /v1/items/7; GET /v2/items/7Working Example
Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use axum::routing::get; let app = Router::new() .route("/v1/items/7", get(|| async { Json(serde_json::json!({"name":"pen"})) })) .route("/v2/items/7", get(|| async { Json(serde_json::json!({"display_name":"pen"})) })); for uri in ["/v1/items/7", "/v2/items/7"] { let res = app.clone().oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap()).await.unwrap(); let data: serde_json::Value = serde_json::from_slice(&to_bytes(res.into_body(), 1024).await.unwrap()).unwrap(); println!("{} {}", uri, data.as_object().unwrap().keys().next().unwrap()); }}