< 返回版块

C-Jeril 发表于 2024-03-18 16:28

报错信息如下:

[{
	"resource": "/e:/Backend/src/routes.rs",
	"owner": "rustc",
	"code": {
		"value": "Click for full compiler diagnostic",
		"target": {
			"$mid": 1,
			"path": "/diagnostic message [2]",
			"scheme": "rust-analyzer-diagnostics-view",
			"query": "2",
			"fragment": "file:///e%3A/Backend/src/routes.rs"
		}
	},
	"severity": 8,
	"message": "the trait bound `fn(axum::Json<AcceptCardPayload>, Extension<Pool<ConnectionManager<PgConnection>>>) -> impl Future<Output = Result<axum::Json<ApiResponse<()>>, axum::http::StatusCode>> {accept_card_handler}: Handler<_, _>` is not satisfied\nthe following other types implement trait `Handler<T, S>`:\n  <axum::handler::Layered<L, H, T, S> as Handler<T, S>>\n  <MethodRouter<S> as Handler<(), S>>",
	"source": "rustc",
	"startLineNumber": 103,
	"startColumn": 42,
	"endLineNumber": 103,
	"endColumn": 61,
	"relatedInformation": [
		{
			"startLineNumber": 103,
			"startColumn": 37,
			"endLineNumber": 103,
			"endColumn": 41,
			"message": "required by a bound introduced by this call",
			"resource": "/e:/Backend/src/routes.rs"
		},
		{
			"startLineNumber": 140,
			"startColumn": 16,
			"endLineNumber": 140,
			"endColumn": 29,
			"message": "required by a bound in `post`",
			"resource": "/c:/Users/Administrator/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.4/src/routing/method_routing.rs"
		}
	]
}]

出现问题的代码login_handler,同类型的get函数都是正常的,一使用post就有问题

// 定义应用路由
pub fn app_router(pool: DbPool) -> Router {
    let cors = configure_cors();

    Router::new()
        .route("/api/login", post(login_handler))
        .route("/api/login", options(options_handler)) // 处理跨域资源共享(CORS)预检请求
        .layer(cors) // 添加跨域资源共享(CORS)中间件
        .layer(Extension(pool)) // 将数据库连接池作为共享状态

    // ... 其他路由 ...
}
// src/login.rs
use crate::auth::{generate_jwt, validate_user_credentials, validate_wechat_user, Claims};
use crate::db::DbPool;
use axum::{extract::Json, http::StatusCode, Extension};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    username: Option<String>,
    password: Option<String>,
    wechat_code: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct LoginResponse {
    token: String,
}

pub async fn login_handler(
    Json(req): Json<LoginRequest>,
    Extension(pool): Extension<DbPool>,
) -> Result<Json<LoginResponse>, StatusCode> {
    // 判断是否有微信code
    if let Some(code) = req.wechat_code {
        // 验证微信用户
        match validate_wechat_user(&code, &pool).await {
            Ok(authenticated_user) => {
                // 生成token
                let claims = Claims::new(authenticated_user.id, 3600);
                let token = generate_jwt(&claims)?;
                Ok(Json(LoginResponse { token }))
            }
            Err(e) => Err(e),
        }
    }
    // 判断是否有用户名和密码
    else if let (Some(username), Some(password)) = (req.username, req.password) {
        // 验证用户名和密码
        match validate_user_credentials(username, password, pool).await {
            Ok(user_id) => {
                // 生成token
                let claims = Claims::new(user_id, 3600);
                let token = generate_jwt(&claims)?;
                Ok(Json(LoginResponse { token }))
            }
            Err(e) => Err(e),
        }
    }
    // 否则返回错误
    else {
        Err(StatusCode::BAD_REQUEST)
    }
}


尝试看了论坛里面另外一个同类报错,但是没找到解决思路

评论区

写评论
作者 C-Jeril 2024-03-19 09:37

已解决,这个问题之前没有研究明白,再次感谢,向您多学习。😄

--
👇
Bai-Jinlin: 好好看报错,你这个问题报错绝对有note: Consider using #[axum::debug_handler] to improve the error message这么一句。

你把#[axum::debug_handler]这个宏放到post的handle上就会提示Json<_> consumes the request body and thus must be the last argument to the handler function,来告诉你把json和extension参数换个位置就好了。

作者 C-Jeril 2024-03-19 09:16

感谢感谢,我马上看

👇
Bai-Jinlin: 好好看报错,你这个问题报错绝对有note: Consider using #[axum::debug_handler] to improve the error message这么一句。

你把#[axum::debug_handler]这个宏放到post的handle上就会提示Json<_> consumes the request body and thus must be the last argument to the handler function,来告诉你把json和extension参数换个位置就好了。

Bai-Jinlin 2024-03-18 16:45

好好看报错,你这个问题报错绝对有note: Consider using #[axum::debug_handler] to improve the error message这么一句。

你把#[axum::debug_handler]这个宏放到post的handle上就会提示Json<_> consumes the request body and thus must be the last argument to the handler function,来告诉你把json和extension参数换个位置就好了。

1 共 3 条评论, 1 页