2

我正在运行一个简单的客户端/服务器 XML-RPC 应用程序以更好地理解 Apache XMLRPC 库。

但我希望能够打印或调试客户端发送的 XML 输出和服务器接收到的 XML。我怎么能找到这些数据?

  //StartServer.java
public class StartServer {
public static void main(String args[]) throws Exception {
    Server server = new Server();
  }
}

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

  public class Server {

    private static final int port = 3000;
    public Server() throws Exception {
      WebServer webServer = new WebServer(port);
      XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
      PropertyHandlerMapping phm = new PropertyHandlerMapping();
      phm.addHandler("reply", ReplyClass.class);
      xmlRpcServer.setHandlerMapping(phm);
      XmlRpcServerConfigImpl serverConfig = 
        (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
      serverConfig.setEnabledForExtensions(true);
      serverConfig.setContentLengthOptional(false);
      webServer.start();
    }
  }

  //ReplyClass.java
  public class ReplyClass {
    public int reply(Object params[]){
      for(int i = 0;  i< params.length; i++){
        System.out.println( "Params :"+params[i].toString());
      }
    return 0;
    }

    public int foo(int x){
      System.out.println("foo : "+ x);
      return 1;
    }

  }



  //XMLRPCtest

  import java.net.URL;
  import org.apache.xmlrpc.XmlRpcException;
  import org.apache.xmlrpc.client.XmlRpcClient;
  import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  import org.apache.xmlrpc.client.XmlRpcLocalTransportFactory;
  import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

  public class XMLRPCtest {
    private XmlRpcClient client;
    public XMLRPCtest() {
    try {
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://localhost:3000"));
        config.setEnabledForExtensions(true);           
        client = new XmlRpcClient();
        client.setConfig(config);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
  }

  public Object execute(String command, Object[] params) {
    try {
      Object reply = client.execute(command, params);
      return reply;
    } catch (XmlRpcException e) {
        e.printStackTrace();
        return null;
    }
  }

  public static void main(String args[]) {
    try {
        XMLRPCtest client = new XMLRPCtest();
        int data[] = {};
        Object[] params = new Object[]{1};
        Integer result = (Integer) client.execute("reply.foo", params);
        System.out.println("Result: " + result);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
  }
}
4

0 回答 0