go调用java,返回值为枚举类型,会报错
luky116 opened this issue · 2 comments
luky116 commented
测试目的
想测试下,dubbo协议下,go-client 和 java-server 的是否互通,包括测试枚举类型、基本类型、结构体等
问题描述
有个测试方法的返回值为枚举类型,java-server能正常返回,但是go-client侧无法解析,并报错
分析
试了下,同样的方法,go-client调用go-server,是能正常调通的。怀疑是不是java和go之间互通的问题
代码改动
go-client侧:GetGender func(i int32) (Gender, error)
改为了GetGender func(ctx context.Context, i int32) (Gender, error)
java-server侧:因为这个例子本来是跑不通的,所以java-server侧的代码几乎都是新加的
关键代码
go-clieng 中枚举类型的定义
type Gender hessian.JavaEnum
const (
MAN hessian.JavaEnum = iota
WOMAN
)
var genderName = map[hessian.JavaEnum]string{
MAN: "MAN",
WOMAN: "WOMAN",
}
var genderValue = map[string]hessian.JavaEnum{
"MAN": MAN,
"WOMAN": WOMAN,
}
func (g Gender) JavaClassName() string {
return "org.apache.dubbo.sample.Gender"
}
func (g Gender) String() string {
s, ok := genderName[hessian.JavaEnum(g)]
if ok {
return s
}
return strconv.Itoa(int(g))
}
func (g Gender) EnumValue(s string) hessian.JavaEnum {
v, ok := genderValue[s]
if ok {
return v
}
return hessian.InvalidJavaEnum
}
java-server侧枚举定义
package org.apache.dubbo.sample;
public enum Gender {
MAN,
WOMAN
}
go-client 侧出错方法:
// TODO cann't call java server
logger.Infof("\n\n\nstart to test dubbo - enum")
gender, err := userProvider.GetGender(context.TODO(), 1)
if err != nil {
panic(err)
}
logger.Infof("response result: %v", gender)
详细代码地址:https://github.com/luky116/dubbo-go-samples/tree/fix-rpcDubbo/rpc/dubbo
wongoo commented
gender, err := userProvider.GetGender(context.TODO(), 1)
这里的最后一个参1 ,应该用 MAN/WOMAN 吧?
luky116 commented