XML-RPC – 在Java中从服务器到客户端抛出异常
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了XML-RPC – 在Java中从服务器到客户端抛出异常,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3442字,纯文字阅读大概需要5分钟。
内容图文
![XML-RPC – 在Java中从服务器到客户端抛出异常](/upload/InfoBanner/zyjiaocheng/808/f3beea1a0e754c7ab3529a5208c0ee57.jpg)
这是我第一次在这里发帖,在搜索过程中找不到我的问题的答案,所以让我看看我是否可以正确解释自己.
我使用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();
然后一切都按预期工作.
内容总结
以上是互联网集市为您收集整理的XML-RPC – 在Java中从服务器到客户端抛出异常全部内容,希望文章能够帮你解决XML-RPC – 在Java中从服务器到客户端抛出异常所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。