Add actix-json-response to your dependencies:
```toml [dependencies]
actix-web = "4" actix-json-response = "0.1" ```
actix-json-response exposes the JsonResponse type which implements Actix's Responder trait. It is generic and receives a type parameter that must implement Serde's Serialize trait:
```rust,compilefail use actixweb::{get, web, Result}; use actixjsonresponse::JsonResponse; use serde::Serialize;
struct MyObj { name: String, }
async fn index(name: web::Path
async fn main() -> std::io::Result<()> { use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().service(index))
.bind(("127.0.0.1", 8080))?
.run()
.await
} ```
By default, the response will have status code 200. If you need the response to have a status code other than 200, you can use the with_status_codemethod that receives an Actix's StatusCode:
``` use actix_web::http::StatusCode;
async fn index(name: web::Path
Distributed under the terms of MIT license and Apache license.