Rust:axum学习笔记(2) response

2023-05-16

Rust:axum学习笔记(2) response

上一篇的hello world里,示例过于简单,仅仅只是返回了一个字符串,实际上axum的response能返回各种格式,包括:

plain_text
html
json
http StatusCode
...
web开发中需要的各种格式,都能返回。talk is cheap ,show me the code! 直接上代码:

1

2

3

4

5

axum = "0.4.3"

tokio = { version="1", features = ["full"] }

serde = { version="1", features = ["derive"] }

serde_json = "1"

http = "0.2.1"

这是依赖项,下面的代码主要来自官方文档,在此基础上补充了中文及“自定义错误”及“自定义结构”的返回示例(包括了web开发中大部分的常用返回格式)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

use axum::{

    body::{self,Body},

    http::header::{HeaderMap, HeaderName, HeaderValue},

    response::{Headers, Html, IntoResponse, Json,Response},

    routing::get,

    Router,

};

use http::{ StatusCode, Uri};

use serde::Serialize;

use serde_json::{json, Value};

// We've already seen returning &'static str

async fn plain_text() -> &'static str {

    "foo"

}

// String works too and will get a `text/plain; charset=utf-8` content-type

async fn plain_text_string(uri: Uri) -> String {

    format!("Hi from {}", uri.path())

}

// Bytes will get a `application/octet-stream` content-type

async fn bytes() -> Vec<u8> {

    vec![1, 2, 3, 4]

}

// `()` gives an empty response

async fn empty() {}

// `StatusCode` gives an empty response with that status code

async fn empty_with_status() -> StatusCode {

    StatusCode::NOT_FOUND

}

// A tuple of `StatusCode` and something that implements `IntoResponse` can

// be used to override the status code

async fn with_status() -> (StatusCode, &'static str) {

    (StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong")

}

// A tuple of `HeaderMap` and something that implements `IntoResponse` can

// be used to override the headers

async fn with_headers() -> (HeaderMap, &'static str) {

    let mut headers = HeaderMap::new();

    headers.insert(

        HeaderName::from_static("x-foo"),

        HeaderValue::from_static("foo"),

    );

    (headers, "foo")

}

// You can also override both status and headers at the same time

async fn with_headers_and_status() -> (StatusCode, HeaderMap, &'static str) {

    let mut headers = HeaderMap::new();

    headers.insert(

        HeaderName::from_static("x-foo"),

        HeaderValue::from_static("foo"),

    );

    (StatusCode::INTERNAL_SERVER_ERROR, headers, "foo")

}

// `Headers` makes building the header map easier and `impl Trait` is easier

// so you don't have to write the whole type

async fn with_easy_headers() -> impl IntoResponse {

    Headers(vec![("x-foo""foo")])

}

// `Html` gives a content-type of `text/html`

async fn html() -> Html<&'static str> {

    Html("<h1>Hello, World!</h1>")

}

// `Json` gives a content-type of `application/json` and works with any type

// that implements `serde::Serialize`

async fn json() -> Json<Value> {

    Json(json!({ "data": 42 }))

}

// `Result<T, E>` where `T` and `E` implement `IntoResponse` is useful for

// returning errors

async fn result() -> Result<&'static str, StatusCode> {

    Ok("all good")

}

// `Response` gives full control

async fn response() -> Response<Body> {

    Response::builder().body(Body::empty()).unwrap()

}

#[derive(Serialize)]

struct Blog {

    title: String,

    author: String,

    summary: String,

}

async fn blog_struct() -> Json<Blog> {

    let blog = Blog {

        title: "axum笔记(2)-response".to_string(),

        author: "菩提树下的杨过".to_string(),

        summary: "response各种示例".to_string(),

    };

    Json(blog)

}

async fn blog_struct_cn() -> (HeaderMap, Json<Blog>) {

    let blog = Blog {

        title: "axum笔记(2)-response".to_string(),

        author: "菩提树下的杨过".to_string(),

        summary: "response各种示例".to_string(),

    };

    let mut headers = HeaderMap::new();

    headers.insert(

        HeaderName::from_static("content-type"),

        HeaderValue::from_static("application/json;charset=utf-8"),

    );

    (headers, Json(blog))

}

struct CustomError {

    msg: String,

}

impl IntoResponse for CustomError {

    fn into_response(self) -> Response {

        let body= body::boxed(body::Full::from(self.msg));

        Response::builder()

            .status(StatusCode::INTERNAL_SERVER_ERROR)

            .body(body)

            .unwrap()

    }

}

async fn custom_error() -> Result<&'static str, CustomError> {

    Err(CustomError {

        msg: "Opps!".to_string(),

    })

}

#[tokio::main]

async fn main() {

    // our router

    let app = Router::new()

        .route("/plain_text", get(plain_text))

        .route("/plain_text_string", get(plain_text_string))

        .route("/bytes", get(bytes))

        .route("/empty", get(empty))

        .route("/empty_with_status", get(empty_with_status))

        .route("/with_status", get(with_status))

        .route("/with_headers", get(with_headers))

        .route("/with_headers_and_status", get(with_headers_and_status))

        .route("/with_easy_headers", get(with_easy_headers))

        .route("/html", get(html))

        .route("/json", get(json))

        .route("/result", get(result))

        .route("/response", get(response))

        .route("/blog", get(blog_struct))

        .route("/blog_cn", get(blog_struct_cn))

        .route("/custom_error", get(custom_error));

    // run it with hyper on localhost:3000

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())

        .serve(app.into_make_service())

        .await

        .unwrap();

}

 部分url的访问效果如上图,其它的就留给朋友们自己去尝试吧。注:网上传说的中文乱码问题,0.4.3新版本并没有出现,输出时已经自带了chatset=utf-8。

最后再补1个返回图片的示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

use axum::{

    routing::get, Router,http::header::{HeaderMap,HeaderValue,HeaderName}

};

use std::fs::*;

async fn image() -> (HeaderMap, Vec<u8>) {

    let mut headers = HeaderMap::new();

    headers.insert(

        HeaderName::from_static("content-type"),

        HeaderValue::from_static("image/png"),

    );

    (headers, read(String::from("/Users/Downloads/avatar.png")).unwrap())

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rust:axum学习笔记(2) response 的相关文章

  • [软件注册]Sublime 3 激活/注册码(key)

    偶然发现了一种sublime激活方式 使用的sublime3 1 1版本 亲试有效 Step1 配置 host文件 推荐使用 switchhost软件 可以快速变更host span class hljs number 127 0 span
  • 测试git能否连接github

    welcome to my blog 使用以下命令进行测试 ssh T git 64 github com 出现报错 ssh dispatch run fatal Connection to 13 250 177 223 port 22 S
  • vtk中实现3D模型(读取文件)

    xff08 xff09 VTK 坐标系统及空间变换 窗口 视图分割 mb5fed73533dfa9的技术博客 51CTO博客 VTK学习 xff08 三 xff09 VTK读取序列图像 灰信网 xff08 软件开发博客聚合 xff09 读取
  • centos中安装Python2.7

    转载于 xff1a 秋水逸冰 CentOS 6 8安装Python2 7 13 查看当前系统中的 Python 版本 python version 返回 Python 2 6 6 为正常 检查 CentOS 版本 cat etc redha
  • 安装tar.gz文件(无configure文件)

    如何安装tar gz文件 xff08 以webstorm为例 xff09 1 获取root权限并输入密码 xff1a su root 2 进入有该文件的目录下 xff08 以我的为例 xff0c 具体看你的文件在哪里 xff09 cd 下载
  • 游戏服务端框架之业务线程模型

    请求消息绑定线程策略的选择 在上一篇文章中 我们看到 消息是直接在网络框架的io线程中处理的 这样做有一个非常严重的缺陷 如果业务处理比较耗时 那么io线程接受消息的速度就会下降 严重影响io的吞吐量 典型的 我们应该另起线程池 专门用于异
  • 在WSL中使用GPU:WSL2 + Ubuntu 18.04 + CUDA + Gnome图形界面环境配置

    目录 引言1 确认Windows 10版本2 在Windows上安装WSL23 在Windows上安装CUDA on WSL驱动4 在WSL2中安装CUDA Toolkit3 测试CUDA是否能在WSL2中运作4 安装Gnome图形界面其他
  • Centos 开启路由转发实现全网互通

    只需在RouterSrv网关服务器上开启路由转发功能即可 root 64 RouterSrv vi etc sysctl conf net ipv4 ip forward 61 1 添加此行即可 root 64 localhost sysc
  • 虚拟机中配置外网环境

    文章目录 在虚拟机中配置外网环境 在虚拟机中配置外网环境 主机为 win10 xff0c 虚拟机中为 ubuntu 系统 xff0c 采用clash 1 xff0c 设置 Allow Lan xff0c 允许局域网访问 2 xff0c 虚拟
  • mysql 操作数据库(备份与恢复)

    一 直接把创建数据库的语句放到sql 文件中 xff1a php 写法 xff1a lt php mysql port 61 get mysql port cmd 61 US MYSQL BIN 34 mysql exe port 61 3
  • Go调用Python by go-python3

    确保python版本为3 7 conda create go python span class token assign left variable python span span class token operator 61 spa
  • linux下搭建maven私服

    maven私服我相信很多公司都有 xff0c 私服的好处有以下几点 xff1a 1 节省下载资源开销 jar包 xff08 不一定是jar xff0c 也可以是其他资源 xff09 都存在私服 xff0c 可以不必每次下载都去远程仓库去下载
  • git 安装包 最新 下载 快速 国内 镜像 地址

    下载git时 xff0c 先进官网看 https git scm com download win 然后发现几kb的网速 xff0c 这是要让我下一年么 xff0c 找了找网上有没有其他的镜像 xff0c 发现阿里有一个镜像 xff0c 下
  • docker笔记(四、docker部署beego打包后的二进制文件)

    在beego工程里 xff0c 使用go build可以将该工程打包成一个二进制文件 xff0c 那么这个二进制文件在docker里面该怎么部署呢 xff1f 先写一个简单的图片上传的demo xff0c 名字叫docker test 在工
  • LINUX服务器最简洁的HTTPS免费证书配置方法

    注意 xff1a 该方法已在多台服务器配置了免费的https证书 xff0c 无论是更新还是第一次配置都运行成功 xff1b 由于是免费版 xff0c 每个证书都只有三个月的有效期 xff0c 也无法保证安全和稳定性 xff0c 所以只建议
  • 【性能测试】获取性能系统指标之示例Python代码

    usr bin env python coding utf 8 import sys import datetime import time import psutil from ctypes import 34 34 34 性能测试示例P
  • select I/O 多路复用实现服务器聊天室功能

    基本概念 IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备读取 xff0c 它就通知该进程 IO多路复用适用如下场合 xff1a xff08 1 xff09 当客户处理多个描述字时 xff08 一般是交互式输入和网络套接口 x
  • iOS-使用Masnory实现UITableViewCell自适应高度

    在iOS开发当中 xff0c 如果涉及到UITableViewCell的一些复杂UI的绘制时难免会碰到这么一个难题 xff1a UITableViewCell的高度如何设置 xff01 的确 xff0c 我们就拿一个简单的例子来说 xff1
  • ubuntu中共享文件夹看不到

    博主的ubuntu安装VMwaretools后共享文件夹设置完发现在 mnt hgfs总看不到 经过多次摸索后终于可以了 首先要使用root用户登陆ubuntu 然后再安装VMwaretools 在设置共享文件夹 然后解决挂在的问题 1 设
  • keystore was tampered with,or password was incorrect解决办法

    利用keytool导入证书 xff0c 命令如下 keytool import alias HZZSQKJdianshang file HZZSQKJdianshang cer keystore trust jks storepass st

随机推荐

  • Convert Picture or Video to ascii

    一个利用ascii拼成的谷歌街景地图 xff01 http tllabs io asciistreetview 看上去效果真不错 xff01 除此之外 xff0c linux下面也有类似的ascii艺术 xff0c 比如 aview asc
  • texlive2015-6安装

    http www cnblogs com snake553 p 4831588 html CentOS6 5 下安装 texlive2015 并设置 ctex 中文套装 0 卸载旧版本的 texlive 0 1 卸载 texlive2007
  • ubuntu20.04 安装显卡驱动后开机卡住,无法进入登陆界面问题解决

    ubuntu20 04 安装显卡驱动后开机卡住 xff0c 无法进入登陆界面问题解决 进入recovery mode 禁用nvidia drm systemctl isolate multi user target modprobe r n
  • Linux环境变量失效,命令不可用

    背景 linux在修改完环境变量 etc profile后保存文件后 xff0c 发现大多数命令不可用 xff0c 只有少数如 xff1a cd pwd可以使用 xff1b 原因分析 1 etc profile文件中有无效字符或变量 xff
  • Ubuntu20.04安装MySQL5.7-实测3种方法(保姆级教程)

    最近生产系统系统需要使用MySQL5 7版本的数据库 xff0c 而Ubuntu20 04默认是8 0的版本 xff0c 折腾了一段时间后 xff0c 测试了3中方法 xff0c 在实际应用环境中测试成功 xff0c 因此发布出来给大家参考
  • 数据仓库的源数据类型

    数据仓库中集成了企业几乎所有的可以获取到的数据以用于数据分析和决策支持 xff0c 当然也包括了我在网站分析的数据来源一文中所提到的所有数据 这些进入到数据仓库中的数据无外乎三种类型 xff1a 结构化数据 半结构化数据 和非结构化数据 x
  • soj 1075 拓扑排序队列实现

    就是soj 拓扑排序的模板题吧 然后我中午把用队列实现的拓扑排序的方法看了下 晚上就打算来练一下这种纯模板 对于这实现的方法 xff0c 我的理解就是存下每个节点的入度以及它指向的其他节点 xff0c 由于指向多少个这个不太能确定所以用一个
  • poker

    include lt stdbool h gt include lt stdio h gt include lt stdlib h gt define NUM RANKS 13 define NUM SUTTS 4 define NUM C
  • vue实现筛选动态配置

    lt el form class 61 34 cellStyleForm 34 model 61 34 queryParams 34 ref 61 34 queryForm 34 v show 61 34 showSearch 34 inl
  • C. Good String Codeforces Round #560 (Div. 3)

    C Good String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outp
  • v4l2接口解析和摄像头数据采集

    V4L2接口解析 操作步骤 应用程序通过V4L2接口采集视频数据步骤 打开视频设备文件 xff0c 通过视频采集的参数初始化 xff0c 通过V4L2接口设置视频图像属性 申请若干视频采集的帧缓存区 xff0c 并将这些帧缓冲区从内核空间映
  • 阿里云cdn缓存命中查看

    我们在使用阿里云想知道CDN缓存命中 命中Hit X Cache HIT TCP MEM HIT dirn 9 435942540 上面的情况是命中的情况 不命中的情况miss X Cache MISS TCP MISS dirn 2 2
  • ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)

    源码下载地址 http download csdn net detail liukang325 9489952 我用的ffmpeg版本为 ffmpeg 2 1 8 tar bz2 版本低了恐怕有些头文件和API找不到 在Linux下解压后编
  • vue的script代码

  • C++扑克牌类的设计

    C 43 43 扑克牌 1 设计扑克类Card xff1a 主要属性有花色和点数 xff1b 主要方法有初始化 输出 比较大小等 2 设计一副扑克牌类Cards xff1a 主要属性包括扑克对象数组 xff1b 主要方法有初始化 洗牌 输出
  • 用Linux系统做路由器

    一 网络结构如下 Internet nbsp nbsp nbsp nbsp nbsp 路由器A Lan 192 168 1 1 nbsp nbsp nbsp nbsp Linux路由器 eth0接路由器A IP 192 168 1 2 网关
  • rust 学习笔记

    按照菜鸟教程 xff1a Rust 教程 菜鸟教程 安装成功了 xff0c 可以跑通第一个程序 中间有一些插曲 xff0c 比如报了 error linker 96 link exe 96 not found 这个错误 输入 xff1a r
  • Rust:axum学习笔记(4) 上传文件

    Rust axum学习笔记 4 上传文件 上传文件是 web开发中的常用功能 xff0c 本文将演示axum如何实现图片上传 xff08 注 xff1a 其它类型的文件原理相同 xff09 xff0c 一般来说要考虑以下几个因素 xff1a
  • vue el-select数据量太大,导致浏览器崩溃解决办法

    下拉数据量太大 xff0c 浏览器单线程渲染时间较长 xff0c 会导致浏览器崩溃 为了解决这一问题 xff0c 可以采用懒加载形式 xff0c 完美解决 lt el col span 61 34 24 34 gt lt el form i
  • Rust:axum学习笔记(2) response

    Rust axum学习笔记 2 response 上一篇的hello world里 xff0c 示例过于简单 xff0c 仅仅只是返回了一个字符串 xff0c 实际上axum的response能返回各种格式 xff0c 包括 plain t