使用 Axis2 Java 的 Web 服务抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/191826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Service throwing exception using Axis2 Java
提问by Vinze
I'm actually developing a Web Service in Java using Axis 2. I designed my service as a POJO (Plain Old Java Object) with public method throwing exceptions :
我实际上正在使用 Axis 2 在 Java 中开发 Web 服务。我将我的服务设计为一个 POJO(Plain Old Java Object),公共方法抛出异常:
public class MyService {
public Object myMethod() throws MyException {
[...]
}
}
I then generated the WSDL using Axis2 ant task. With the WSDL I generate a client stub to test my service. The generated code contains a "MyExceptionException" and the "myMethod" in the stub declare to throw this :
然后我使用 Axis2 ant 任务生成了 WSDL。使用 WSDL,我生成了一个客户端存根来测试我的服务。生成的代码在存根声明中包含一个“MyExceptionException”和“myMethod”来抛出这个:
public class MyServiceStub extends org.apache.axis2.client.Stub {
[...]
public MyServiceStub.MyMethodResponse myMethod(MyServiceStub.MyMethod myMethod)
throws java.rmi.RemoteException, MyExceptionException0 {
[...]
}
[...]
}
But when calling the method surrounded by a catch, the "MyExceptionException" is never transmitted by the server which transmit an AxisFault instead (subclass of RemoteException).
但是,当调用由 catch 包围的方法时,“MyExceptionException”永远不会由传输 AxisFault 的服务器(RemoteException 的子类)传输。
I assume the problem is server-side but don't find where. The service is deployed as an aar file in the axis2 webapp on a tomcat 5.5 server. The services.xml looks like this :
我认为问题出在服务器端,但找不到在哪里。该服务作为 aar 文件部署在 tomcat 5.5 服务器上的axis2 webapp 中。services.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<service name="MyService" scope="application">
<description></description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass">MyService</parameter>
<parameter name="ServiceTCCL">composite</parameter>
</service>
If the behavior is normal then I'll drop the use of Exceptions (which is not vital to my project) but I'm circumspect why Java2WSDL generate custom <wsdl:fault> in operation input & output declaration and WSDL2Java generate an Exception class (and declare to throw it in the stub method) if this is not usable...
如果行为是正常的,那么我将放弃使用 Exceptions(这对我的项目来说并不重要)但我很谨慎为什么 Java2WSDL 在操作输入和输出声明中生成自定义 <wsdl:fault> 并且 WSDL2Java 生成一个 Exception 类(并声明将其扔到存根方法中)如果这不可用...
回答by extraneon
I don't really think there is a problem. Your Client calls a method on the server. That method results in an exception. Axis transforms this exception to something which can be send to the client to indicate the error.
我真的不认为有问题。您的客户端调用服务器上的方法。该方法导致异常。Axis 将此异常转换为可以发送到客户端以指示错误的内容。
All exceptions, as far as I know, are wrapped into an AxisFault which is then transmitted to the client as, I believe, a SoapFault message with as description the exception message.
据我所知,所有异常都被包装到一个 AxisFault 中,然后将其作为 SoapFault 消息传输到客户端,我相信它是一个带有异常消息描述的 SoapFault 消息。
In other words, the client should only see AxisFaults as the exception (exception class) is not serialized and send. Server exceptions should become AxisFaults at the client side.
换句话说,客户端应该只看到 AxisFaults 作为异常(异常类)未序列化和发送。服务器异常应该成为客户端的 AxisFaults。
回答by Axel
Have you tried using Axis2 with Lady4j, it solved this issue for us.
您是否尝试过将 Axis2 与 Lady4j 一起使用,它为我们解决了这个问题。
回答by Russell
If your WSDL specifies that your service throws a custom error your client should expect to handle these errors as well as the generic remote exceptions thrown by the operation of Axis2.
如果您的 WSDL 指定您的服务抛出自定义错误,您的客户端应该期望处理这些错误以及 Axis2 操作抛出的一般远程异常。
When your stub recieves an AxisFault from the server, it attempts to consturct a custom exception if this is specified in your WSDL. If this fails it will simply pass out the AxisFault instead.
当您的存根从服务器收到 AxisFault 时,它会尝试构建自定义异常(如果这在您的 WSDL 中指定)。如果失败,它只会传递 AxisFault。
The stub will attempt to call f.getDetail(). If this is null it will not try to construct a custom exception and will pass out the AxisFault. With Axis2 1.5, the autogenerated MessageInOutReciver on the serverside does not set this value by default.
存根将尝试调用 f.getDetail()。如果这是 null,它不会尝试构建自定义异常,而是会传递 AxisFault。在 Axis2 1.5 中,服务器端自动生成的 MessageInOutReciver 不会默认设置此值。
You can set it manually on the serverside like this (assuming you have autogenerated MyFaultException and MyFault classes):
您可以像这样在服务器端手动设置它(假设您已自动生成 MyFaultException 和 MyFault 类):
MyFaultException ex = new MyFaultException("My Exception Message");
MyFault fault = new MyFault();
fault.setMyFault("My Fault Message");
ex.setFaultMessage(fault);
throw ex;

