rust trait `Responder`未为`Vec `实现< T>

m2xkgtsf  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(111)

我在一个学习项目中使用actix_web,在尝试创建路由时遇到以下错误:

the trait `Responder` is not implemented for `Vec<TextAnnotation>`

字符串
这是生成错误的代码:

use std::str::FromStr;

use actix_web::{http::StatusCode, web::Json, Result};
use mongodb::{
    bson::{doc, oid::ObjectId},
    options::{FindOneAndUpdateOptions, FindOptions, ReturnDocument},
};

use crate::{
    database::mongodb::DB,
    helpers::colors_helpers::{get_next_valid_color, is_color_used, is_valid_color},
    middleware::auth_middleware::UserAuthContext,
    models::text_annotation_model::{
        CreateLabelBody, CreateTextAnnotationBody, CreateTokenBody, Label, TextAnnotation,
        UpdateLabelBody,
    },
    object::{common::Message, error::ApiError},
};

// ...

#[get("/")]
async fn get_annotations(
       // ^ the trait `Responder` is not implemented for `Vec<TextAnnotation>`
    page: web::Query<i64>,
    count: web::Query<i64>,
    req: HttpRequest,
) -> Result<Vec<TextAnnotation>, ApiError> {
    let auth = get_auth_ctx(&req);

    let res = AnnotationController::get_page(page.0, count.0, auth);

    if res.is_err() {
        return Err(res.err().unwrap());
    }

    Ok(res.unwrap())
}


TextAnnotation及其内部字段已经实现了Responder trait。

use actix_web::{
    get, post,
    web::{self},
    HttpRequest, Result, Scope,
};

use crate::{
    controllers::annotation_controller::AnnotationController,
    helpers::request_helpers::get_auth_ctx,
    models::text_annotation_model::{CreateTextAnnotationBody, TextAnnotation},
    object::error::ApiError,
};

// ...

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TextAnnotation {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub content: String,
    pub user_id: ObjectId,
    pub tokens: Vec<Token>,
    pub labels: Vec<Label>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Token {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub start: i64,
    pub end: i64,
    /// reference a label in the labels array
    pub label: ObjectId,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Label {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub name: String,
    pub color: String,
}

impl Responder for TextAnnotation {
    type Body = BoxBody;

    fn respond_to(self, _req: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
        HttpResponse::Ok().json(self)
    }
}

impl Responder for Label {
    type Body = BoxBody;

    fn respond_to(self, _req: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
        HttpResponse::Ok().json(self)
    }
}

impl Responder for Token {
    type Body = BoxBody;

    fn respond_to(self, _req: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
        HttpResponse::Ok().json(self)
    }
}

toiithl6

toiithl61#

感谢@cdhowie
我所需要做的就是从actix_web::web导入Json,并将值返回为Json<Vec<T>>,就是这样。

相关问题