java - XML-RPC - 在 Java 中从服务器向客户端抛出异常

标签 java exception-handling xml-rpc

这是我第一次在这里发帖,我在搜索过程中找不到我的问题的答案,所以让我们看看我能否正确解释自己。

我正在使用 XML-RPC 作为大型项目的一部分,但我将提供一个简化的代码,我在其中获得了相同的结果。

如果我不抛出异常,连接工作正常。我的问题是从服务器向客户端抛出异常。我在客户端收到 XmlRpcException,但其原因始终为 null。看起来异常在传输中丢失了。知道为什么吗?

我的服务器:

public class JavaServer { 

public Integer sum(int x, int y) throws Exception {
throw new MineException("ABABBABA");
}

public static void main (String [] args) {
try {

 System.out.println("Attempting to start XML-RPC Server...");
 WebServer server = new WebServer(80);
 XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
 PropertyHandlerMapping phm = new PropertyHandlerMapping();
 phm.addHandler("test", JavaServer.class);
 xmlRpcServer.setHandlerMapping(phm);
 XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
    serverConfig.setEnabledForExceptions(true);

 server.start();
 System.out.println("Started successfully.");
 System.out.println("Accepting requests. (Halt program to stop.)");
} catch (Exception exception) {
 System.err.println("JavaServer: " + exception);
}}}

我的客户:

public static void main(String[] args) {

    XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
    try {
        config.setServerURL(new URL("http://localhost:80"));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);           
        Object[] params = new Object[] { new Integer(38), new Integer(3), };

        Integer result = (Integer) client.execute("test.sum", params);
        System.out.println("Results " + result);
    } catch (XmlRpcException exception) {
        Throwable cause = exception.getCause();
        if(cause != null) {
            if(cause instanceof MineException) {
                System.out.println(((MineException)cause).getMessage());
            }
            else { System.out.println("Cause not instance of Exception"); }
        }
        else { System.out.println("Cause was null"); }
        } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

最佳答案

我发现通过在客户端和服务器上使用 apache xml-rpc 实现,可以从服务器抛出自定义异常并在客户端接收它们。只需要在服务器和客户端配置中添加一些东西。

服务器:

WebServer server = new WebServer(80);
 XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
 PropertyHandlerMapping phm = new PropertyHandlerMapping();
 phm.addHandler("test", JavaServer.class);
 xmlRpcServer.setHandlerMapping(phm);
 XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
    serverConfig.setEnabledForExceptions(true);
    serverConfig.setEnabledForExtensions(true); //Add this line
 server.start();

客户:

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
    try {
        config.setServerURL(new URL("http://localhost:80"));
        config.setEnabledForExceptions(true);
        config.setEnabledForExtensions(true); //Add this line
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);                           
        Object[] params = new Object[] { new Integer(11), new Integer(3), };
        Integer result = (Integer) client.execute(config,"test.sum", params);
        System.out.println("Results " + result);
    } catch (XmlRpcException exception) {
        System.out.println(exception.getMessage());
        Throwable cause = exception.getCause();
        if(cause != null) {
            if(cause instanceof MyException) {
                System.out.println(((MyException)cause).getMessage());
            }
            else { System.out.println("Cause not instance of Exception."); }
        }
        else { System.out.println("Cause was null."); }
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

然后一切都按预期工作。

关于java - XML-RPC - 在 Java 中从服务器向客户端抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5952348/

相关文章:

c# - 在 C# 中使用异常抛出。对性能有影响吗?

python - odoo v9 enterprice saas 与 xml-rpc 连接

php - 哪个 XML-RPC 库?

c# - 无法使用其他语言的其他客户端连接到 WCF-XMLRPC-Server

java - 跳过try/catch block 以检查异常的Lambda

java - 在 Java 或 Groovy 中,如何找到 byte[] 的文件类型?

c# - .NET 错误处理

windows - 异常处理

java - 使用javac编译一个简单的java程序

java - 数字签名智能卡密码错误反馈