You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在axum-typed-routing中使用axum-login?遇Trait绑定错误求解决

Axum中api_route宏与axum-login兼容报错问题

按照axum-login仓库示例完成实现后,使用axum-typed-routing的api_route宏定义接口时出现编译错误,代码如下:

#[api_route(GET "/item/:id?amount&offset&test" with AppState {
    summary: "Get an item",
    description: "Get an item by id",
    id: "get-item",
    tags: ["items"],
    hidden: false
    security: { "auth_session": ["read:items"] },
    responses: { 200: String },
})]
async fn item_handler(
    id: u32,
    amount: Option<u32>,
    offset: Option<u32>,
    test: Option<u32>,
    State(state): State<AppState>,
    Json(json): Json<u32>,
    auth_session: AuthSession,
) -> String {
    ...
}

报错信息:

error[E0277]: the trait bound `fn(axum::extract::Path<(u32,)>, axum::extract::Query<__QueryParams__>, axum::extract::State<AppState>, extractors::Json<u32>, axum_login::AuthSession<users::Backend>) -> impl std::future::Future<Output = std::string::String> {__inner__function__}: OperationHandler<_, _>` is not satisfied
   --> src/main.rs:120:1
    |
120 | / #[api_route(GET "/item/:id?amount&offset&test" with AppState {
121 | |     summary: "Get an item",
122 | |     description: "Get an item by id",
123 | |     id: "get-item",
...   |
127 | |     responses: { 200: String },
128 | | })]
    | |___^ the trait `OperationHandler<_, _>` is not implemented for fn item `fn(axum::extract::Path<(u32,)>, axum::extract::Query<__QueryParams__>, axum::extract::State<AppState>, extractors::Json<u32>, axum_login::AuthSession<users::Backend>) -> impl std::future::Future<Output = std::string::String> {__inner__function__}`
    |
note: required by a bound in `get_with`
   --> /Users/gradlon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/aide-0.13.2/src/axum/routing.rs:345:1
    |
345 | method_router_top_level!(get, get_with);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------^
    | |                             |
    | |                             required by a bound in this function
    | required by this bound in `get_with`
= note: this error originates in the attribute macro `api_route` which comes from the expansion of the macro `method_router_top_level` (in Nightly builds, run with -Z macro-backtrace for more info)

问题原因与修复步骤

核心问题分析

  1. GET请求与JSON提取器冲突:HTTP规范中GET请求不应携带请求体,api_route依赖的aide库会严格校验这种不规范的提取器组合,直接导致OperationHandler trait无法匹配。
  2. AuthSession未被aide识别:axum-login的AuthSession提取器默认没有实现aide的OperationHandler trait,无法被api_route宏处理。
  3. 宏语法错误:hidden: false后缺少逗号,导致宏解析失败,间接加剧了错误。

修复步骤

  1. 修正请求方法或移除JSON提取器
    如果需要接收JSON数据,将请求方法改为POST;如果是纯GET请求,删除Json<u32>提取器,改用查询参数传递数据。

    修改后的api_route示例(改为POST):

    #[api_route(POST "/item/:id?amount&offset&test" with AppState {
        summary: "Get an item",
        description: "Get an item by id",
        id: "get-item",
        tags: ["items"],
        hidden: false,
        security: { "auth_session": ["read:items"] },
        responses: { 200: String },
    })]
    
  2. 标记AuthSession为忽略文档的提取器
    使用aide提供的#[skip_handler]属性,告诉宏忽略该提取器的OpenAPI文档生成逻辑:

    // 先导入宏
    use aide::axum::skip_handler;
    
    async fn item_handler(
        id: u32,
        amount: Option<u32>,
        offset: Option<u32>,
        test: Option<u32>,
        State(state): State<AppState>,
        Json(json): Json<u32>,
        #[skip_handler] auth_session: AuthSession,
    ) -> String {
        // ... 业务逻辑
    }
    
  3. 修复宏语法错误
    在hidden: false后添加逗号,确保宏能正确解析配置项:

    hidden: false,
    security: { "auth_session": ["read:items"] },
    

内容的提问来源于stack exchange,提问作者Gradlon von Kaenel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:00:21