gin注解路由,自动参数绑定工具

ginprc

golang gin 参数自动绑定工具

  • 支持rpc自动映射
  • 支持对象注册
  • 支持注解路由
  • 基于 go-gin 的 json restful 风格的golang基础库
  • 自带请求参数过滤及绑定实现 binding:“required” validator
  • 代码注册简单且支持多种注册方式

api接口说明

支持3种接口模式

  • func(*gin.Context) //go-gin 原始接口

    func(*api.Context) //自定义的context类型

  • func(*api.Context,req) //自定义的context类型,带request 请求参数

    func(*api.Context,*req)

  • func(*gin.Context,*req) //go-gin context类型,带request 请求参数

    func(*gin.Context,req)

一,参数自动绑定

  package main

import (
	"fmt"
	"net/http"

	_ "ginweb/routers" // debug模式需要添加[mod]/routers 注册注解路由

	"github.com/gin-gonic/gin"
	"github.com/xxjwxc/ginrpc"
	"github.com/xxjwxc/ginrpc/api"
)

type ReqTest struct {
	Access_token string `json:"access_token"`
	UserName     string `json:"user_name" binding:"required"` // 带校验方式
	Password     string `json:"password"`
}

//TestFun4 带自定义context跟已解析的req参数回调方式
func TestFun4(c *gin.Context, req ReqTest) {
	fmt.Println(c.Params)
	fmt.Println(req)

	c.JSON(http.StatusOK, req)
}

func main() {
	base := ginrpc.New() 
	router := gin.Default()
	router.POST("/test4", base.HandlerFunc(TestFun4))
	base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun4) // 多种请求方式注册
	router.Run(":8080")
}

  • curl

    curl 'http://127.0.0.1:8080/test4' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
    
    

二,对象注册(注解路由)

初始化项目(本项目以ginweb 为名字)

```go mod init ginweb ```

代码 详细地址>>

  package main

import (
	"fmt"
	"net/http"

	_ "ginweb/routers" // debug模式需要添加[mod]/routers 注册注解路由

	"github.com/gin-gonic/gin"
	"github.com/xxjwxc/ginrpc"
	"github.com/xxjwxc/ginrpc/api"
)

type ReqTest struct {
	Access_token string `json:"access_token"`
	UserName     string `json:"user_name" binding:"required"` // 带校验方式
	Password     string `json:"password"`
}

// Hello ...
type Hello struct {
}

// Hello 带注解路由(参考beego形式)
// @router /block [post,get]
func (s *Hello) Hello(c *api.Context, req *ReqTest) {
	fmt.Println(req)
	fmt.Println(s.Index)
	c.JSON(http.StatusOK, "ok")
}

// Hello2 不带注解路由(参数为2默认post)
func (s *Hello) Hello2(c *gin.Context, req ReqTest) {
	fmt.Println(req)
	fmt.Println(s.Index)
	c.JSON(http.StatusOK, "ok")
}


func main() {
	base := ginrpc.New(ginrpc.WithCtx(func(c *gin.Context) interface{} {
		return api.NewCtx(c)
	}), ginrpc.WithDebug(true))

	router := gin.Default()
	group := router.Group("/xxjwxc")
	base.Register(group, new(Hello))                          // 对象注册 like(go-micro)
	// or base.Register(router, new(Hello)) 
	router.Run(":8080")
}

-注解路由相关说明

 // @router /block [post,get]

@router 标记 

/block 路由
 
[post,get] method 调用方式

1. 注解路由会自动创建[mod]/routers/gen_router.go 文件 需要在调用时加:

```
_ "[mod]/routers" // debug模式需要添加[mod]/routers 注册注解路由

```

默认也会在项目根目录生成[gen_router.data]文件(保留此文件,可以不用添加上面代码嵌入)

2. 注解路由调用方式:

详细请看demo  [ginweb](/sample/ginweb)

3. 相关参数说明

ginrpc.WithCtx : 设置自定义context

ginrpc.WithDebug(true) : 设置debug模式

ginrpc.WithGroup("xxjwxc") : 添加路由前缀 (也可以使用gin.Group 分组)

ginrpc.WithBigCamel(true) : 设置大驼峰标准(false 为web模式,_,小写)

[更多](https://godoc.org/github.com/xxjwxc/ginrpc)

4. 执行curl,可以自动参数绑定。直接看结果

curl 'http://127.0.0.1:8080/xxjwxc/block' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
curl 'http://127.0.0.1:8080/xxjwxc/hello.hello2' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'

下一步

1.导出api文档

2.导出postman测试配置

代码地址: ginprc 如果喜欢请给星支持

Gin框架中,参数绑定是将HTTP请求中的数据绑定到Go结构体中的过程。Gin框架支持多种参数绑定方式,包括将查询字符串参数绑定到结构体字段、将POST表单数据绑定到结构体字段、将JSON数据绑定到结构体字段等。 以下是一个示例,展示如何在Gin框架中使用参数绑定: ```go type User struct { Name string `form:"name"` Password string `form:"password"` } func main() { r := gin.Default() r.POST("/login", func(c *gin.Context) { var user User if err := c.ShouldBind(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // TODO: 验证用户名和密码 c.JSON(http.StatusOK, gin.H{"message": "登录成功"}) }) r.Run() // 启动服务 } ``` 在上面的示例中,我们定义了一个`User`结构体,并使用`form`标签指定了每个字段对应的查询字符串参数名。在处理`/login`路由时,我们使用`ShouldBind`方法将HTTP请求中的数据绑定到`User`结构体中,如果绑定失败,则返回一个错误响应。如果绑定成功,则可以使用`User`结构体中的字段进行用户名和密码验证,并返回成功响应。 需要注意的是,Gin框架使用了`ShouldBind`方法来实现参数绑定,这个方法会自动根据HTTP请求的Content-Type字段来选择绑定方式。如果Content-Type为application/json,则会将JSON数据绑定到结构体中;如果Content-Type为application/x-www-form-urlencoded,则会将POST表单数据绑定到结构体中,以此类推。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值