【问题标题】:Custom faultcode using Axis2使用 Axis2 的自定义故障代码
【发布时间】:2011-04-03 19:20:13
【问题描述】:

我创建了一个 web 服务并使用 Axis2 生成所有“骨架”java 类。然后我当然是自己实现了服务操作。

在实现中,我可以抛出一个 MyException,然后它会被生成的类捕获并转换为 AxisFault 对象,该对象又会转换为带有属性<faultcode>soapenv:Server</faultcode> 的肥皂错误(在 Axis 框架的深处)

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

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

是否可以在输出上获得某种钩子或过滤器,以便我可以更改故障代码?或者有什么其他方法可以控制故障码?

提前致谢
乌尔里克

【问题讨论】:

    标签: web-services soap axis2


    【解决方案1】:

    SOAP specification 描述了自定义故障信息如何显示在 detail 标记下。 faultcode 是一组固定值,用于处理在 SOAP 处理中引发错误的位置。

    以下是抛出自定义故障信息的示例

    WSDL

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

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

    服务代码

    以下代码演示了如何抛出自定义错误消息:

    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 消息

    <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 错误响应

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

    【讨论】:

    • 马克,在您的示例中,SOAP 错误代码将设置为什么?如果你能发布一个完整的肥皂响应来显示一个错误的例子,那就太好了。
    • 自定义故障信息出现在故障detail标签下。 SOAP faultcode 是一组固定的值。这是文档w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383510
    • 好的。所以即使我找到了方法我也不应该更改故障代码:) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 2023-03-07
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多