1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-24 22:37:35 +02:00

added Json response support

This commit is contained in:
Nikolay Kim
2017-12-03 18:51:52 -08:00
parent 5decff9154
commit 319e9bbd05
4 changed files with 67 additions and 4 deletions

View File

@ -23,7 +23,7 @@ fn main() {
## Directory
To serve all files from specific directory `StaticFiles` type could be used.
To serve files from specific directory and sub-directories `StaticFiles` type could be used.
`StaticFiles` could be registered with `Application::route` method.
```rust
@ -36,6 +36,6 @@ fn main() {
}
```
First parameter is a base directory. Second parameter is `show_index`, if it set to *true*
First parameter is a base directory. Second parameter is *show_index*, if it is set to *true*
directory listing would be returned for directories, if it is set to *false*
then *404 Not Found* would be returned instead of directory listing.

View File

@ -13,3 +13,29 @@ Following encodings are supported:
If request headers contains `Content-Encoding` header, request payload get decompressed
according to header value. Multiple codecs are not supported, i.e: `Content-Encoding: br, gzip`.
## JSON Response
The `Json` type allows you to respond with well-formed JSON data: simply return a value of
type Json<T> where T is the type of a structure to serialize into *JSON*. The
type `T` must implement the `Serialize` trait from *serde*.
```rust
extern crate actix_web;
#[macro_use] extern crate serde_derive;
use actix_web::*;
#[derive(Serialize)]
struct MyObj {
name: String,
}
fn index(req: HttpRequest) -> Result<Json<MyObj>> {
Ok(Json(MyObj{name: req.match_info().query("name")?}))
}
fn main() {
Application::default("/")
.resource(r"/a/{name}", |r| r.get(index))
.finish();
}
```