2011-05-10 204 views
4

这是我第一次在这里张贴和搜索过程中我找不到答案,我的问题所以让我们看看我是否能正确地解释一下。XML-RPC - 引发异常从服务器到客户端在Java中

我使用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(); 
    } 
} 

回答

3

我发现通过在客户端和服务器上使用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(); 

然后一切按预期工作。

+0

自定义异常不是xml-rpc标准的一部分。这就是为什么apache的实现提供这种可能性作为实际协议的扩展。因此,您必须在apache xml-rpc中启用扩展,并且它仅适用于使用它的客户端和服务器。 – Otto 2011-06-04 20:47:41

+0

您好奥托,感谢您的评论和是的,的确,我发现通过启用扩展我可以做我想做的。 – Augusto 2011-06-09 15:58:11

2

有点调查我找到了答案后...简短的答案是,它是不可能通过发送自定义异常XMLRPC通道根据XMLRPC spec

结构是否可以包含除faultCode之外的其他成员a nd faultString?

故障结构可能不包含非指定的成员。所有其他结构都是如此。我们相信该规范足够灵活,以便所有合理的数据传输需求都可以在指定的结构中进行调整。如果您坚信这不是真的,请在讨论组上发布信息。

不是那么简短的答案是,如果服务器抛出XmlRpcException异常,则可以用另一种方式来做。

public Integer sum(int x, int y) throws XmlRpcException { 
    // XmlRpcException(exceptionCode,exceptionMessage) 
    throw new XmlRpcException(1,"Message"); 
} 

而在客户端:

catch (XmlRpcException exception) { 
    System.out.println("Exception code: " + exception.code); 
    System.out.println("Exception message: " + exception.getMessage()); 
} 

如果客户端和服务器同意使用定义的代码,异常可以通过代码和消息来识别。这不是真正的自定义例外,但它是我们可以达到的最接近的例外。

3

其他一些片(希望有用)的建议。

所以,你想启用异常扩展,但是你正在一个Servlet内部(一个完整的)web服务器(比如say Apache Tomcat)中运行服务。然后,您需要通过在web.xml文件(WEB-INF \ web)中配置它们来启用扩展和例外。xml):

<servlet> 
    <servlet-name>My_XMLRPC_Servlet</servlet-name> 
    <servlet-class>com.stackoverflow.server.MyXmlRpcServlet</servlet-class> 
     <init-param> 
     <param-name>enabledForExtensions</param-name> 
     <param-value>true</param-value> 
     </init-param> 
     <init-param> 
     <param-name>enabledForExceptions</param-name> 
     <param-value>true</param-value> 
     </init-param> 
     ... 
</servlet> 
+0

“扩展的扩展名” - 这是一个错字或初始? – 2012-11-14 09:24:58

+0

我的意思是扩展例外。所以一个错字。 :-) – adcelis 2012-11-19 12:50:41

相关问题