加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang(12)Web Service - JSON Mapping Improvement

发布时间:2020-12-16 18:52:32 所属栏目:大数据 来源:网络整理
导读:Golang(12)Web Service - JSON Mapping Improvement Take the marshal and unmarshal codes out of every method. package main import ( "encoding/json" "fmt" " github.com/gorilla/mux " "io/ioutil" "net/http" ) type Bug struct { Id string BugNumbe

Golang(12)Web Service - JSON Mapping Improvement

Take the marshal and unmarshal codes out of every method.

package main

import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
)

type Bug struct {
Id string
BugNumber string
BugName string
BugDesn string
}

type JSONResult map[string]interface{}

func (r JSONResult) String() (s string) {
b,err := json.Marshal(r)
if err != nil {
s = "json err: " + err.Error()
return
}
s = string(b)
return
}

func ParseJSON(r *http.Request) Bug {
var b Bug
c,_ := ioutil.ReadAll(r.Body)
fmt.Println("ParseJSON called with Body=" + string(c))

json.Unmarshal(c,&b)
return b
}

func getBug(w http.ResponseWriter,r *http.Request) {
b1 := Bug{Id: "1",BugNumber: "bug1",BugName: "bug1",BugDesn: "desn1"}

w.Header().Set("Content-Type","application/json")
fmt.Fprint(w,JSONResult{"status": "ok","bugs": []Bug{b1}})
}

func updateBug(w http.ResponseWriter,r *http.Request) {
b := ParseJSON(r)

fmt.Println("updateBug called with Id=" + b.Id)
fmt.Println("updateBug called with BugNumber=" + b.BugNumber)
fmt.Println("updateBug called with BugName=" + b.BugName)
fmt.Println("updateBug called with BugDesn=" + b.BugDesn)

w.Header().Set("Content-Type","bugs": []Bug{b}})
}

func deleteBug(w http.ResponseWriter,r *http.Request) {
id := mux.Vars(r)["Id"]
fmt.Println("deleteBug called with Id = ",id)

w.Header().Set("Content-Type",JSONResult{"status": "ok"})
}

func addBug(w http.ResponseWriter,r *http.Request) {
b := ParseJSON(r)

fmt.Println("addBug called with BugNumber=" + b.BugNumber)
fmt.Println("addBug called with BugName=" + b.BugName)
fmt.Println("addBug called with BugDesn=" + b.BugDesn)

w.Header().Set("Content-Type","bugs": []Bug{b}})
}

func listBug(w http.ResponseWriter,BugDesn: "desn1"}
b2 := Bug{Id: "2",BugNumber: "bug2",BugName: "bug2",BugDesn: "desn2"}

w.Header().Set("Content-Type","bugs": []Bug{b1,b2}})
}

func main() {
router := mux.NewRouter()
router.HandleFunc("/bugs",listBug).Methods("GET")
router.HandleFunc("/bugs",addBug).Methods("POST")
router.HandleFunc("/bugs/{Id}",getBug).Methods("GET")
router.HandleFunc("/bugs/{Id}",updateBug).Methods("PUT")
router.HandleFunc("/bugs/{Id}",deleteBug).Methods("DELETE")

http.Handle("/",router)
http.ListenAndServe(":8088",nil)
}

Maybe,it will be helpful if one day I design and implement a web framework myself. Actually,it will be light and clean,only need routers,dispatcher and JSON render and parse built-in the framework.

References:
http://nesv.blogspot.com/2012/09/super-easy-json-http-responses-in-go.html
http://www.alexedwards.net/blog/golang-response-snippets#json

http://sillycat.iteye.com/blog/2056435
http://golangtutorials.blogspot.com/2011/06/methods-on-structs.html
http://stackoverflow.com/questions/18678135/go-static-method-design

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读