英文:
golang jsonrpc call java json rpc
问题
我正在尝试通过JSON-RPC将Golang程序与Java程序连接起来,但遇到了一些问题。我在Golang中使用了"net/rpc/jsonrpc"包,而在Java中使用了"jsonrpc4j"。我在Java中使用流模式创建了一个服务器,因为Golang的JSON-RPC只支持TCP调用。但是在Golang客户端中仍然收到了错误响应:
> 错误:无效的错误映射[code:-32602 message:Invalid method parameters]
与此同时,Java服务器也遇到了错误:
> com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: com.googlecode.jsonrpc4j.NoCloseInputStream@385b5b5c; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3747)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3687)
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2202)
at com.googlecode.jsonrpc4j.JsonRpcServer.handle(JsonRpcServer.java:224)
at com.googlecode.jsonrpc4j.StreamServer$Server.run(StreamServer.java:214)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
我不明白为什么它不起作用,是不是JSON-RPC协议中存在某种规定,导致Golang无法调用Java,还是我做错了什么?
英文:
I was trying to connect golang programs with java programs by json rpc, but ran into some trouble. I use the "net/rpc/jsonrpc" package from golang native packages, and "jsonrpc4j" for java. I use stream mode to create a server in java, because golang's jsonrpc just supports tcp invoke. But it still gets the error response in golang client:
> err: invalid error map[code:-32602 message:Invalid method parameters]
Meanwhile the java server is also encountering error:
> com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: com.googlecode.jsonrpc4j.NoCloseInputStream@385b5b5c; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3747)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3687)
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2202)
at com.googlecode.jsonrpc4j.JsonRpcServer.handle(JsonRpcServer.java:224)
at com.googlecode.jsonrpc4j.StreamServer$Server.run(StreamServer.java:214)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
I don't understand why it does not work, is there any discipline that exists in the jsonrpc protocol that golang can't invoke java or do I just not do it right?
答案1
得分: 0
我希望我能给出一个更好的答案。这是我之前尝试过但无法实现的东西。我读到只有Go客户端才能与Go服务器进行rpc通信。
在尝试让Java和Go之间的json/rpc工作失败后,我采用了两种方法,也许你可以根据自己的需求进行类似的调整。
在一种情况下,我使用了net/http包,在Go中创建了一个基本的Web服务器,然后使用HandlerFunc根据URL请求调用函数/方法。这些请求基于简单的HTTP表单操作。然后,我重定向回主页面,因为我希望“应用程序”始终在用户面前,我只需要“/submit” URL触发一个带有从表单传递的值的方法。
虽然不够优雅,但它在紧急情况下起作用,可以快速设置一种收集信息的方式,并且通过告诉我的同事我监听的端口,可以立即“部署”。
在第二种情况下,由于我的一个朋友在编写Java端,我设置了一个套接字并监听我们约定的特定字节模式,以调用特定的函数。
虽然不太灵活,但如果你知道从客户端获取的内容,这种方法也可以。
英文:
I wish I had a better answer. This is something that I was trying to make work earlier and couldn't get to happen. I've read that only Go clients can rpc to Go servers.
There are two means which I resorted to after trying to make json/rpc work between Java and Go, maybe you can tailor something similar to your needs.
In one case I used the net/http package and did a basic web server in Go and then used HandlerFunc to call functions/methods based on url requests. The requests were based on simple http form actions. I then redirected back to the main page as I wanted the "app" to stay in front of the user as I just needed the "/submit" url to trigger a method with the values passed in from the form.
Not all that elegant but it worked in a pinch to rapidly set up a way to gather information and it could be instantly "deployed" by letting my colleagues know what port I was listening on.
In the second case, since a friend of mine was programming the Java side I set up a socket a listed for a pattern of bytes that we had agreed on which would call a particular function.
Not so adaptable but if you what what you're getting from the client it works out fine.
专注分享java语言的经验与见解,让所有开发者获益!
评论