使用 Axis2 Java Web 服务抛出异常

发布于 2024-07-06 08:02:27 字数 1658 浏览 9 评论 0原文

我实际上正在使用 Axis 2 用 Ja​​va 开发 Web 服务。 我将我的服务设计为具有抛出异常的公共方法的 POJO(普通旧 Java 对象):

public class MyService {
   public Object myMethod() throws MyException {
        [...]
   }
}

然后使用 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 {
        [...]
    }
    [...]
}

但是当调用被 catch 包围的方法时,服务器永远不会传输“MyExceptionException”,而是传输 AxisFault(RemoteException 的子类) )。

我认为问题出在服务器端,但没有找到哪里。 该服务作为 aar 文件部署在 tomcat 5.5 服务器上的 axis2 web 应用程序中。 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>

如果行为正常,那么我将放弃使用异常(这对我的项目并不重要),但我很谨慎为什么 Java2WSDL 生成自定义; 在操作输入& 如果这不可用,输出声明和 WSDL2Java 会生成一个 Exception 类(并声明将其抛出到存根方法中)...

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 :

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 :

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).

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 :

<?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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-07-13 08:02:27

我真的不认为有什么问题。 您的客户端调用服务器上的方法。 该方法会导致异常。 Axis 将此异常转换为可以发送到客户端以指示错误的内容。

据我所知,所有异常都被包装到 AxisFault 中,然后将其作为 SoapFault 消息传输到客户端,其中包含异常消息的描述。

换句话说,客户端应该只看到 AxisFaults,因为异常(异常类)未序列化和发送。 服务器异常应该成为客户端的 AxisFaults。

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.

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.

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.

暮光沉寂 2024-07-13 08:02:27

您是否尝试过将 Axis2 与 Lady4j 一起使用,它为我们解决了这个问题。

Have you tried using Axis2 with Lady4j, it solved this issue for us.

窝囊感情。 2024-07-13 08:02:27

如果您的 WSDL 指定您的服务抛出自定义错误,您的客户端应该期望处理这些错误以及 Axis2 操作抛出的通用远程异常。

当您的存根从服务器收到 AxisFault 时,它会尝试构造自定义异常(如果您的 WSDL 中已指定)。 如果失败,它只会传递 AxisFault。

存根将尝试调用 f.getDetail()。 如果为 null,则不会尝试构造自定义异常并传递 AxisFault。 对于 Axis2 1.5,服务器端自动生成的 MessageInOutReciver 默认情况下不会设置此值。

您可以在服务器端手动设置它,如下所示(假设您有自动生成的 MyFaultException 和 MyFault 类):

        MyFaultException ex = new MyFaultException("My Exception Message");
        MyFault fault = new MyFault();
        fault.setMyFault("My Fault Message");
        ex.setFaultMessage(fault);
        throw ex; 

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.

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.

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.

You can set it manually on the serverside like this (assuming you have autogenerated MyFaultException and MyFault classes):

        MyFaultException ex = new MyFaultException("My Exception Message");
        MyFault fault = new MyFault();
        fault.setMyFault("My Fault Message");
        ex.setFaultMessage(fault);
        throw ex; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文