最近在使用grpc协议的时候,因为采用的是Proto3协议,在查找记录信息的时候,因为某些字段会有默认空值,致使在经过协议调用后,返回的json结构中并无这些字段,虽然做为前端使用没有太大的问题,可是在更多的使用场景中,咱们更须要知道该服务返回的确切字段,以便于可以作相应处理,尤为是编译型语言前端
具体的使用出现场景以下json
type MemberResponse struct { Id int32 `json "id"` Phone string `json "phone"` Age int8 `json "age"` } //获取用户信息的接口 func (m *Member) GetMember(req *proto.MemberRequest, resp * proto.MemberResponse) error { resp.Phone = "15112810201" resp.Id = 12 return nil }
当经过api调用该微服务后,在proto3协议下,会返回以下结果:api
{ "phone" : "15112810201", "id" : 12 }
此时就会出现空值的Age
字段没有返回到对应的json结构中,而这样在某些状况下对前端也是不太友好的,尤为是APP客户端,更须要明确的json响应字段结构,那么咱们能够怎么处理这个问题呢,通过研究和网上的解答,有两种办法:app
member.pb.go
文件代码,删除掉不但愿被忽略的字段tag标签中的omitempty
便可,可是*.pb.go通常咱们不建议去修改它,并且咱们会常常去调整grpc微服务协议中的方法或者字段内容,这样每次protoc以后,都须要咱们去修改,这显然是不太现实的,所以就有了第二种办法;jsonpb
来实现,官方在它的设定中有一个结构体用来实现protoc buffer
转换为JSON结构,并能够根据字段来配置转换的要求,结构体以下:// Marshaler is a configurable object for converting between // protocol buffer objects and a JSON representation for them. type Marshaler struct { // 是否将枚举值设定为整数,而不是字符串类型. EnumsAsInts bool // 是否将字段值为空的渲染到JSON结构中 EmitDefaults bool //缩进每一个级别的字符串 Indent string //是否使用原生的proto协议中的字段 OrigName bool }
了解了这个结构体以后呢,咱们就开始对应的使用办法:
经过Marshaler
结构体的Marshal
方法,实现了将proto响应的内容转化为buffer
,最终输出为JSON结构,从而实现了空值字段的返回微服务
import ( member "proto/member" ) var jsonpbMarshaler *jsonpb.Marshaler func queryHandler(req *http.Requst, resp http.ResponseWriter){ var ( _buffer bytes.Buffer ) memberResponse, err := member.GetMember(context.TODO(), &member.MemberRequest{}) //调用此方法实现转换 jsonpbMarshaler.Marshal(&_buffer, memberResponse) jsonCnt := _buffer.Bytes() resp.Header().Set('Content-Type', 'application/json') resp.Write(jsonCnt) return } func main(){ jsonpbMarshaler = &jsonpb.Marshaler{ EnumsAsInts : true, EmitDefaults: true, OrigName : true } //其余http处理代码块 }
写这篇文章只是为了吧这个解决方案分享给你们,但愿对有此疑问的朋友们有所帮助。jsonp