我想在java中创建一个xmlrpc
服务器,其方法是获取一个Map参数。服务器将由python客户端访问,
这是服务器代码:
import org.apache.xmlrpc.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class JavaServer {
public static Object getData(Map <String, String> test){
// Map <String, String> hm = new HashMap<String, String>();
// hm.put("test", "result");
// Object result = hm;
return "ok";
};
public static void main (String [] args) {
try {
System.out.println("Attempting to start XML-RPC Server...");
WebServer server = new WebServer(8080);
server.addHandler("sum", new JavaServer());
server.start();
System.out.println("Started successfully.");
System.out.println("Accepting requests. (Halt program to stop.)");
} catch (Exception exception) {
System.err.println("JavaServer: " + exception);
}
}
}
客户端代码:
>>> import xmlrpclib
>>> proxy = xmlrpclib.ServerProxy("http://localhost:8080/")
>>> proxy.sum.getData({"a":"b"})
我收到了错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request
verbose=self.__verbose
File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request
return self.single_request(host, handler, request_body, verbose)
File "/usr/lib/python2.7/xmlrpclib.py", line 1297, in single_request
return self.parse_response(response)
File "/usr/lib/python2.7/xmlrpclib.py", line 1473, in parse_response
return u.close()
File "/usr/lib/python2.7/xmlrpclib.py", line 793, in close
raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 0: 'java.lang.NoSuchMethodException: JavaServer.getData(java.util.Hashtable)'>
xmlrpclib.Fault: <Fault 0: 'java.lang.NoSuchMethodException: JavaServer.getData(java.util.Hashtable)'>
如果我改为
,同样的问题public static Object getData(HashMap <String, String> test)
答案 0 :(得分:2)
我在当地做了一个快速原型。顺便说一句,我正在使用Apache的XMLRPC库,因为您使用的是我认为它已被弃用的请参阅here
好的,我制作了一个快速原型并使用maven带来了xmlrpc依赖项。
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xmlrpc-testing</groupId>
<artifactId>xmlrpc-testing</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-common</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-server</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
</project>
Java服务器类 - 观察MyModel类,可以使用您正在使用的内容进行更改
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;
import java.io.IOException;
public class XMLRPCServerMain {
public static void main(String[] args) throws XmlRpcException, IOException {
WebServer webServer = new WebServer(8080);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("MyModel", MyModel.class);
xmlRpcServer.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig =
(XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
webServer.start();
}
}
现在最重要的一点是,getData
方法的定义
public Object getData(Hashtable<String, String> data)
正如我在评论中提到的,您应该尝试使用java.util.Hashtable
。
我认为抛出的异常有点清楚:
xmlrpclib.Fault: <Fault 0: 'java.lang.NoSuchMethodException: JavaServer.getData(java.util.Hashtable)'>