XMLRPC简单示例

1、客户端实现

package com.neohope.xmlrpc.test;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

/**
 * Created by Hansen
 */
public class TestClient {
    public static void main(String[] args) throws MalformedURLException, XmlRpcException {
        //设置服务地址
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://localhost:8080/XmlRpcTest"));
        //config.setServerURL(new URL("http://localhost:1234/xmlrpc"));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);

        //调用方法
        Vector<String> params1 = new Vector<String>();
        params1.addElement("neohope");
        String result1 = (String) client.execute("ITest.sayHiTo", params1);
        System.out.println(result1);

        Vector<Integer> params2 = new Vector<Integer>();
        params2.addElement(1);
        params2.addElement(2);
        int result2 = (int) client.execute("ITest.add", params2);
        System.out.println(result2);
    }
}

2、服务端接口定义

package com.neohope.xmlrpc.test;

/**
 * Created by Hansen
 */
public interface ITest{
    /**
     * 返回“Hi XXX"字符串,必须声明抛出RemoteException异常
     * @return 返回“Hi XXX"字符串
     */
    public String sayHiTo(String user);

    /**
     * 加法,必须声明抛出RemoteException异常
     * @param a
     * @parma b
     * @return a+b
     */
    public int add(int a, int b);
}

3、基于WebServer的服务端实现

package com.neohope.xmlrpc.test;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

/**
 * Created by Hansen
 */
public class TestServer implements ITest {
    /**
     * 返回“Hi XXX"字符串
     * @return 返回“Hi XXX"字符串
     * @throws java.rmi.RemoteException
     */
    @Override
    public String sayHiTo(String user) {
        return "Hi " + user;
    }

    /**
     * 加法
     * @param a
     * @parma b
     * @return a+b
     * @throws java.rmi.RemoteException
     */
    @Override
    public int add(int a, int b) {
        return a+b;
    }


    public static void main(String [] args) throws Exception {
        WebServer webServer = new WebServer(1234);
        XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
        PropertyHandlerMapping phm = new PropertyHandlerMapping();
        phm.addHandler("ITest", TestServer.class);
        xmlRpcServer.setHandlerMapping(phm);

        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)xmlRpcServer.getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false);
        webServer.start();

    }
}

4、基于Servlet的服务端实现

package com.neohope.xmlrpc.test;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.XmlRpcServletServer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Hansen
 */
public class TestServlet extends HttpServlet {
    private XmlRpcServletServer rpcserver;

    public void init(ServletConfig pConfig) throws ServletException {

        super.init(pConfig);

        try {
            rpcserver = new XmlRpcServletServer();
            PropertyHandlerMapping phm = new PropertyHandlerMapping();
            phm.addHandler("ITest", TestServer.class);
            rpcserver.setHandlerMapping(phm);

            XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)rpcserver.getConfig();
            serverConfig.setEnabledForExtensions(true);
            serverConfig.setContentLengthOptional(false);

        } catch (XmlRpcException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException, ServletException {
        rpcserver.execute(pRequest, pResponse);
    }

    @Override
    public void doGet(HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException, ServletException {
        pResponse.getOutputStream().print("It is working.");
    }
}

4、基于Servlet的服务端配置

    <servlet>
        <servlet-name>XmlRpcTest</servlet-name>
        <servlet-class>com.neohope.xmlrpc.test.TestServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>XmlRpcTest</servlet-name>
        <url-pattern>/XmlRpcTest</url-pattern>
    </servlet-mapping>