首页 > 解决方案 > LoopBack 4:向 SOAP 请求中的元素添加类型属性

问题描述

我正在使用 LoopBack 4 访问第三方 SOAP API,并且很难让特定的 SOAP 调用正常工作。该文档似乎没有涵盖这种情况。

WSDL 的相关部分:

<xs:element name="change_item">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="0" name="change" nillable="true" type="q1:item_change" xmlns:q1="http://schemas.datacontract.org/2004/07/configuration_services"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
...
<xs:complexType name="auto_adjust_item">
    <xs:complexContent mixed="false">
        <xs:extension base="tns:item_change">
            <xs:sequence/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:element name="auto_adjust_item" nillable="true" type="tns:auto_adjust_item"/>
<xs:complexType name="item_change">
    <xs:sequence/>
</xs:complexType>
<xs:element name="item_change" nillable="true" type="tns:item_change"/>

以下是从 Java 演示应用程序发送的工作请求中的元素:

<change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:auto_adjust_item"/>

对于我尝试的所有内容,这就是 LoopBack 发送到 SOAP API 的内容(返回错误):

<ns1:change/>

我最初尝试创建类,auto_adjust_item扩展item_change,两者都没有任何属性。

lb4 model item_change然后我使用and将它们创建为模型lb4 model auto_adjust_item,它变成了ItemChangeand AutoAdjustItemAutoAdjustItem已更改为扩展ItemChange而不是Model. 我弄乱了@model注释参数,没有任何帮助。

的实例AutoAdjustItem通过参数定义为的服务接口传递change: ItemChange

有谁知道如何将type="auto_adjust_item"属性添加到 SOAP 请求中的元素?

标签: typescriptsoaploopbackjsloopback4strong-soap

解决方案


知道了!

参数定义:

change: {
    $attributes: {
        $xsiType: string
    }
}

将此作为参数传递:

const change = {
    $attributes: {
        $xsiType: '{http://schemas.datacontract.org/2004/07/configuration_services}auto_adjust_item'
    }
}

更好的是,将其创建为一个类:

export class autoAdjustItem {
    $attributes = {
        $xsiType: '{http://schemas.datacontract.org/2004/07/configuration_services}auto_adjust_item'
    }
}

const change = new autoAdjustItem();

服务器接受的 SOAP 请求中的元素:

<ns1:change xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://schemas.datacontract.org/2004/07/configuration_services" xsi:type="ns2:auto_adjust_item"/>

这感觉像是硬编码命名空间的一个糟糕的解决方法,但它有效,我会接受它。由于严重缺乏支持,我在这一功能上花费了足够的时间。

我通过查看这个文档得到了尝试这个的想法,它看起来与教程让我做的事情非常不同。


推荐阅读