responder
用于将Body转换为对应的字节数据,并设置响应头。默认的处理为将struct(map)转换为json,对于不同的应用可以指定Marshal与ContentType来实现自定义响应。
ResponderConfig.Marshal
自定义的Marshal函数,默认为json.Marshal
ResponderConfig.ContentType
自定义的ContentType,默认为application/json; charset=utf-8
Example
package main
import (
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
)
func main() {
e := elton.New()
e.Use(middleware.NewDefaultResponder())
// {"name":"tree.xie","id":123}
e.GET("/", func(c *elton.Context) (err error) {
c.Body = &struct {
Name string `json:"name"`
ID int `json:"id"`
}{
"tree.xie",
123,
}
return
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}