func main() {
r := gin.Default()
// gin.H 是 map[string]interface{} 的一种快捷方式
r.GET("/someJSON", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})
r.GET("/moreJSON", func(c *gin.Context) {
// 你也可以使用一个结构体
var msg struct {
Name string `json:"user"`
Message string
Number int
}
msg.Name = "Lena"
msg.Message = "hey"
msg.Number = 123
// 注意 msg.Name 在 JSON 中变成了 "user"
// 将输出:{"user": "Lena", "Message": "hey", "Number": 123}
c.JSON(http.StatusOK, msg)
})
r.GET("/someXML", func(c *gin.Context) {
c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})
r.GET("/someYAML", func(c *gin.Context) {
c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})
r.GET("/someProtoBuf", func(c *gin.Context) {
reps := []int64{int64(1), int64(2)}
label := "test"
// protobuf 的具体定义写在 testdata/protoexample 文件中。
data := &protoexample.Test{
Label: &label,
Reps: reps,
}
// 请注意,数据在响应中变为二进制数据
// 将输出被 protoexample.Test protobuf 序列化了的数据
c.ProtoBuf(http.StatusOK, data)
})
// 监听并在 0.0.0.0:8080 上启动服务
r.Run(":8080")
}
使用Response参数您可以Response在路径操作函数中声明类型参数。然后您可以在该时间响应对象中设置 cookie。from fastapi import...
您可以在JSON模式中定义额外的信息。一个常见的用例是添加一个将在文档中显示的example。有几种方法可以声明额外的 JSON 模式信...
FastAPI 支持同时使用File和Form定义文件和表单字段。说明接收上传文件或表单数据,要预先安装python-multipart。例如,pip inst...
通过向规则参数添加变量部分,可以动态构建URL。此变量部分标记为variable-name。它作为关键字参数传递给与规则相关联的函数。在...
Web API 的工作通常与 HTTP 动词紧密相关,所以这使得实现这样一个基于MethodView类的 API 很有意义。也就是说,你会注意到 ...
为了在请求/响应周期之外测试基于类的视图,你必须确保它们配置正确,在实例化之后调用 setup()。例如,假设基于类的视图如...
简介dynamic-datasource-spring-boot-starter是一个基于springboot的快速集成多数据源的启动器。其支持Jdk1.7+,Spring...
动态SQL中的插入脚本语言MyBatis从3.2版本开始支持插入脚本语言,这允许你插入一种语言驱动,并基于这种语言来编写动态SQL查询语...
Spring 有可能成为所有企业应用程序的一站式服务点,然而,Spring 是模块化的,允许你挑选和选择适用于你的模块,不必要把剩余部...
JSP指令用来设置整个JSP页面相关的属性,如网页的编码方式和脚本语言。语法格式如下: %@ directive attribute="value" %指令可...