I am trying to learn basics of XML-RPC
protocol and implementing sample services using Apache xml-rpc library
by following documentation at http://ws.apache.org/xmlrpc/types.html.
So far I have successfully set up a Calculator service which receives two integers and returns sum of integers in response. I am able to call this service using Dynamic Proxy
client. Both service and client classes are in same eclipse project on my machine.
But issue is coming when I am trying to return my own custom data type. Here is how classes look like.
Main Calculator class
public class CalculatorImpl implements Calculator {
@Override
public int sum(int num1, int num2) {
return num1 + num2;
}
@Override
public CalculatorResponse getResponse(int num1, int num2) {
return new CalculatorResponse(num1, num2);
}
}
Service Class
public static void main(String[] args) throws Exception {
XmlRpcServlet servlet = new XmlRpcServlet();
ServletWebServer webServer = new ServletWebServer(servlet, port);
XmlRpcServerConfigImpl serverConfig =
(XmlRpcServerConfigImpl) webServer.getXmlRpcServer().getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setEnabledForExceptions(true);
webServer.start();
}
Dynamic Proxy Client
public CalculatorServiceDynamicProxyClient() throws Exception {
BasicConfigurator.configure();
XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
clientConfig.setServerURL(new URL(SERVICE_STRING_ENDPOINT));
clientConfig.setEnabledForExtensions(true);
clientConfig.setEnabledForExceptions(true);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(clientConfig);
client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
clientFactory = new ClientFactory(client);
}
public int getSum(int num1, int num2) throws XmlRpcException {
Calculator cal = (Calculator) clientFactory.newInstance(Calculator.class);
return cal.sum(num1, num2);
}
public CalculatorResponse getResponse(int num1, int num2) {
Calculator cal = (Calculator) clientFactory.newInstance(Calculator.class);
return cal.getResponse(num1, num2);
}
public static void main(String[] args) throws Exception {
CalculatorServiceDynamicProxyClient client = new CalculatorServiceDynamicProxyClient();
System.out.println(client.getSum(4, 5)); //works
System.out.println(client.getResponse(4, 5)); //throws exception
}
CalculatorResponse
object just holds two integers and implements Serializable
.
Error: org.apache.xmlrpc.client.XmlRpcClientException: to parse server's response: Premature end of file.
I am using 3.1.3
version of xmlrpc-server
and xmlrpc-client
jar.
Any suggestions would be helpful.
Thanks