使用Axis2的自定义故障代码 [英] Custom faultcode using Axis2

查看:94
本文介绍了使用Axis2的自定义故障代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Web服务,并使用Axis2生成了所有骨架" Java类.然后,我当然可以自己执行服务操作.

I've created a webservice and used Axis2 to generate all "skeleton" java classes. Then I of course implemented the service operations myself.

在实现中,我可以抛出MyException,然后将其捕获到生成的类中并转换为AxisFault对象,然后将其转换为具有属性<faultcode>soapenv:Server</faultcode>的肥皂故障(在Axis框架中深入).

In the implementation, I can throw a MyException which is then caught by the generated classes and converted to an AxisFault object, which in turn is converted to a soap fault (deep down in the Axis framework) with the attribute <faultcode>soapenv:Server</faultcode>

我的问题是我想要一个自定义的动态故障代码,而不是"soapenv:Server".

My problem is I would like a custom dynamic faultcode, not "soapenv:Server".

我试图手动创建一个AxisFault对象并抛出该对象,但是AxisFault是RemoteException,并且实现必须实现的生成接口不允许抛出RemoteException.

I tried to manually create an AxisFault object and throw this, but AxisFault is a RemoteException, and the generated interface which my implementation must implement, does not allow to throw RemoteException.

是否可以在输出上获取某种挂钩或过滤器,以便更改故障代码?还是通过其他方式控制故障代码?

Is it possible to get some kind of hook or filter on the output, so that I can change the faultcode? Or any other way to control the faultcode?

预先感谢
乌里克

Thanks in advance
Ulrik

推荐答案

The SOAP specification describes how custom fault information appears under the detail tag. The faultcode is a fixed set of value dealing with where in the SOAP processing the error was thrown.

以下是引发自定义故障消息的示例

The following is an example of throwing a custom fault message

在WSDL中声明错误,以便生成关联的类:

Declare the faults in your WSDL so that the associated classes are generated:

<wsdl:definitions targetNamespace="http://example"
    xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:tns="http://example"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:types>
        <schema elementFormDefault="qualified" targetNamespace="http://example"
            xmlns="http://www.w3.org/2001/XMLSchema"
            xmlns:apachesoap="http://xml.apache.org/xml-soap"
            xmlns:tns="http://example" xmlns:intf="http://example"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

            <element name="withdraw">
                <complexType>
                    <sequence>
                        <element name="account" type="xsd:string"/>
                        <element name="amount" type="xsd:int"/>
                    </sequence>
                </complexType>
            </element>

            <element name="withdrawResponse">
                <complexType>
                    <sequence>
                        <element name="balance" type="xsd:int"/>
                    </sequence>
                </complexType>
            </element>

            <element name="AccountNotExistFault">
                <complexType>
                    <sequence>
                        <element name="account" type="xsd:string"/>
                    </sequence>
                </complexType>
            </element>

            <element name="InsufficientFundFault">
                <complexType>
                    <sequence>
                        <element name="account" type="xsd:string"/>
                        <element name="balance" type="xsd:int"/>
                        <element name="requestedFund" type="xsd:int"/>
                    </sequence>
                </complexType>
            </element>

        </schema>
    </wsdl:types>

    <wsdl:message name="withdrawRequest">
        <wsdl:part element="tns:withdraw" name="parameters"/>
    </wsdl:message>

    <wsdl:message name="withdrawResponse">
        <wsdl:part element="tns:withdrawResponse" name="return"/>
    </wsdl:message>

    <wsdl:message name="InsufficientFundFaultMessage">
        <wsdl:part element="tns:InsufficientFundFault" name="fault"/>
    </wsdl:message>

    <wsdl:message name="AccountNotExistFaultMessage">
        <wsdl:part element="tns:AccountNotExistFault" name="fault"/>
    </wsdl:message>

    <wsdl:portType name="Bank">
        <wsdl:operation name="withdraw">
            <wsdl:input message="tns:withdrawRequest" name="withdrawRequest"/>
            <wsdl:output message="tns:withdrawResponse" name="withdrawResponse"/>
            <wsdl:fault message="tns:AccountNotExistFaultMessage" name="AccountNotExistException"/>
            <wsdl:fault message="tns:InsufficientFundFaultMessage" name="InsufficientFundException"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="BankSoapBinding" type="tns:Bank">
        <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="withdraw">
            <wsdlsoap:operation soapAction=""/>
            <wsdl:input name="withdrawRequest">
                <wsdlsoap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="withdrawResponse">
                <wsdlsoap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="InsufficientFundException">
                <wsdlsoap:fault name="InsufficientFundException" use="literal"/>
            </wsdl:fault>
            <wsdl:fault name="AccountNotExistException">
                <wsdlsoap:fault name="AccountNotExistException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="BankService">
        <wsdl:port binding="tns:BankSoapBinding" name="Bank">
            <wsdlsoap:address location="http://localhost:8080/bank/services/Bank"/>
        </wsdl:port>
    </wsdl:service>

</wsdl:definitions>

服务代码

以下代码演示了如何引发自定义故障消息:

Service code

The following code demonstrates how the custom fault messages are thrown:

package example;

public class BankServiceSkeleton {

    public  WithdrawResponse withdraw(Withdraw param1) throws InsufficientFundFaultMessage, AccountNotExistFaultMessage {

        //
        // Parameter handling
        //
        String account = param1.getAccount();
        int amount     = param1.getAmount();

        //
        // Error checks
        //
        if ("13".equals(account)) {
            AccountNotExistFault fault = new AccountNotExistFault();

            fault.setAccount(account);

            AccountNotExistFaultMessage ex = new AccountNotExistFaultMessage("Account does not exist!");
            ex.setFaultMessage(fault);
            throw ex;
        }

        if (amount > 1000) {
            InsufficientFundFault fault = new InsufficientFundFault();

            fault.setAccount(account);
            fault.setBalance(1000);
            fault.setRequestedFund(amount);

            InsufficientFundFaultMessage ex = new InsufficientFundFaultMessage("Insufficient funds");
            ex.setFaultMessage(fault);
            throw ex;
        }

        //
        // Normal response
        //
        WithdrawResponse response = new WithdrawResponse();

        response.setBalance(1000 - amount);

        return response;
    }
}

测试

以下SOAP消息

TESTING

The following SOAP message

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:exam="http://example">
   <soapenv:Header/>
   <soapenv:Body>
      <exam:withdraw>
         <exam:account>10</exam:account>
         <exam:amount>2000</exam:amount>
      </exam:withdraw>
   </soapenv:Body>
</soapenv:Envelope>

生成以下SOAP错误响应

Generates the following SOAP fault response

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Server</faultcode>
         <faultstring>Insufficient funds</faultstring>
         <detail>
            <ns1:InsufficientFundFault xmlns:ns1="http://example">
               <ns1:account>10</ns1:account>
               <ns1:balance>1000</ns1:balance>
               <ns1:requestedFund>2000</ns1:requestedFund>
            </ns1:InsufficientFundFault>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

这篇关于使用Axis2的自定义故障代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆