【问题标题】:How can I pass a json schema as data to actix web?如何将 json 模式作为数据传递给 actix web?
【发布时间】:2026-01-22 11:35:01
【问题描述】:

我想将预编译的 json 模式传递给 actix web,但编译器抱怨用于创建 JSONSchema 的借用 Value 寿命不够长。有没有办法解决这个问题?

例子:

use jsonschema::JSONSchema;
use serde_json::from_str;
use actix_web::{web, get, App, HttpServer, HttpResponse, Responder};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let schema_str = include_str!("schema.json");
        let schema_value = from_str(schema_str).unwrap();
        let schema_compiled = JSONSchema::compile(&schema_value).unwrap();
        App::new()
            .data(schema_compiled) // fixme: compiles if commented out
            .service(index)
    })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}

#[get("/")]
async fn index<'a>(_schema: web::Data<JSONSchema<'a>>) -> impl Responder {
    HttpResponse::Ok().finish() // todo: use schema for something
}

来自 rustc 的错误:

error[E0597]: `schema_value` does not live long enough
  --> src/main.rs:10:51
   |
10 |         let schema_compiled = JSONSchema::compile(&schema_value).unwrap();
   |                               --------------------^^^^^^^^^^^^^-
   |                               |                   |
   |                               |                   borrowed value does not live long enough
   |                               argument requires that `schema_value` is borrowed for `'static`
...
14 |     })
   |     - `schema_value` dropped here while still borrowed

我是 rust 新手,如果这是一个伪装的通用 rust 问题,我深表歉意(一旦我的理解有所提高,我很乐意用更小的可重复性修改问题)。

【问题讨论】:

标签: rust jsonschema actix-web serde-json


【解决方案1】:

问题的根本原因是JSONSchema 不拥有Value,但我们可以解决这个问题。首先,我们使用Box::newValue 放在堆栈上。然后我们使用Box::leak 泄漏一个引用(它将在应用程序的整个生命周期内持续)。最后我们使用Arc::new,这样我们就可以在内部范围内的模式上调用clone()(这最后一步允许您将模式代码移动到其他地方,这很好)。

use jsonschema::JSONSchema;
use serde_json::{from_str, Value};
use actix_web::{web, get, App, HttpServer, HttpResponse, Responder};
use std::sync::Arc;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let schema_str = include_str!("schema.json");
    let schema_value:  &'static Value = Box::leak(Box::new(from_str(schema_str).unwrap()));
    let schema_compiled: JSONSchema<'static> = JSONSchema::compile(schema_value).unwrap();
    let schema_arc = Arc::new(schema_compiled);
    HttpServer::new(move || {
        App::new()
            .data(schema_arc.clone())
            .service(index)
    })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}

#[get("/")]
async fn index<'a>(_schema: web::Data<Arc<JSONSchema<'a>>>) -> impl Responder {
    HttpResponse::Ok().finish() // todo: use schema for something
}

【讨论】: