美文网首页
第二十五章 SOAP 错误处理 - 发生故障时添加其他标头元素

第二十五章 SOAP 错误处理 - 发生故障时添加其他标头元素

作者: Cache技术分享 | 来源:发表于2024-06-11 07:17 被阅读0次

第二十五章 SOAP 错误处理 - 发生故障时添加其他标头元素

发生故障时添加其他标头元素

除了上一节中讨论的选项之外,或者替代上一节中讨论的选项,Web 服务可以在发生故障时添加自定义标头元素。具体操作如下:

  1. 在创建 %SOAP.Header的子类。在此子类中,添加属性以包含附加数据。
  2. Web 服务的故障处理中(如本主题前面所述),包括以下附加步骤:

a. 创建标题子类的一个实例。

注意:尽管该类的名称如此,但该对象实际上是 SOAP 标头元素,而不是整个标头。SOAP 消息有一个标头,其中包含多个元素。

b. 根据需要设置其属性。

c. 将此标头元素插入 Web 服务的 FaultHeaders 数组属性。为此,请调用该属性的 SetAt()。提供的键将用作主标头元素名称。

例如,考虑以下自定义标题类:

Class Fault.CustomHeader Extends %SOAP.Header
{

Parameter XMLTYPE = "CustomHeaderElement";

Property SubElement1 As %String;

Property SubElement2 As %String;

Property SubElement3 As %String;

}

我们可以修改前面显示的 Web 方法,如下所示:

Method DivideAlt(arg1 As %Numeric, arg2 As %Numeric) As %Numeric [ WebMethod ]
{
  Try {
    Set ans=arg1 / arg2
    } Catch {

        //<detail> element must contain element(s) or whitespace
        //specify this element by passing valid XML as string argument to MakeFault() 
        set mydetail="<mymessage>Division error detail</mymessage>"

        set fault=..MakeFault($$$FAULTServer,"Division error",mydetail)

        //Set fault header
        Set header=##class(CustomHeader).%New()
        Set header.SubElement1="custom fault header element"
        Set header.SubElement2="another custom fault header element"
        Set header.SubElement3="yet another custom fault header element"
        Do ..FaultHeaders.SetAt(header,"CustomFaultElement")

        // ReturnFault must be called to send the fault to the client.
        // ReturnFault will not return here.
        Do ..ReturnFault(fault)
      }
  Quit ans
}

Web 客户端调用 Divide() Web 方法并使用 0 作为分母时,Web 服务响应如下:

<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='https://schemas.xmlsoap.org/soap/envelope/' 
xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance' 
xmlns:s='https://www.w3.org/2001/XMLSchema' xmlns:flt="https://myfault.org" >
  <SOAP-ENV:Header>
<CustomHeaderElement xmlns:hdr="https://www.mynamespace.org">
    <SubElement1>custom fault header element</SubElement1>
    <SubElement2>another custom fault header element</SubElement2>
    <SubElement3>yet another custom fault header element</SubElement3>
</CustomHeaderElement>
</SOAP-ENV:Header>
  <SOAP-ENV:Body>
...

此处添加了换行符以提高可读性。

相关文章

网友评论

      本文标题:第二十五章 SOAP 错误处理 - 发生故障时添加其他标头元素

      本文链接:https://www.haomeiwen.com/subject/cwuwqjtx.html