From c51c7c75218481783d2eac544cb97d17df75028c Mon Sep 17 00:00:00 2001 From: "Robert G. Jakabosky" Date: Thu, 10 Sep 2020 13:11:31 +0800 Subject: [PATCH] Improve JsonConfig docs. --- src/types/json.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/types/json.rs b/src/types/json.rs index ab7978dff..d6b146b94 100644 --- a/src/types/json.rs +++ b/src/types/json.rs @@ -211,6 +211,42 @@ where /// /// # Examples /// +/// Global extractor configuration: +/// ```rust +/// use actix_web::{error, web, App, FromRequest, HttpResponse}; +/// use serde_derive::Deserialize; +/// +/// #[derive(Deserialize)] +/// struct Info { +/// username: String, +/// } +/// +/// /// deserialize `Info` from request's body, max payload size is 4kb +/// async fn index(info: web::Json) -> String { +/// format!("Welcome {}!", info.username) +/// } +/// +/// fn main() { +/// let app = App::new().service( +/// web::resource("/index.html") +/// .app_data( +/// // Json extractor configuration for this resource. +/// web::JsonConfig::default() +/// .limit(4096) // Limit request payload size +/// .content_type(|mime| { // <- accept text/plain content type +/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN +/// }) +/// .error_handler(|err, req| { // <- create custom error response +/// error::InternalError::from_response( +/// err, HttpResponse::Conflict().finish()).into() +/// }) +/// ) +/// .route(web::post().to(index)) +/// ); +/// } +/// ``` +/// +/// Per-type extractor configuration: /// ```rust /// use actix_web::{error, web, App, FromRequest, HttpRequest, HttpResponse}; /// use serde_derive::Deserialize; @@ -242,7 +278,7 @@ where /// let app = App::new().service( /// web::resource("/index.html") /// .app_data( -/// // change json extractor configuration +/// // Json extractor configuration for only the `Info` struct. /// web::Json::::configure(|cfg| { /// cfg.limit(4096) /// .content_type(|mime| { // <- accept text/plain content type