如何在NestJS中使用SOAP服务?

Sus*_*rav 7 xml soap web-services nestjs

我必须为我的一个项目使用 NestJs 中的 SOAP 服务。我正在尝试从网络使用虚拟 SOAP 服务。以下是详细信息——

网址 -

http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
Run Code Online (Sandbox Code Playgroud)

方法-POST

标头 -

Content-Type: text/xml
Run Code Online (Sandbox Code Playgroud)

请求正文 -

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <CapitalCity xmlns="http://www.oorsprong.org/websamples.countryinfo">
            <sCountryISOCode>IN</sCountryISOCode>
        </CapitalCity>
    </Body>
</Envelope>
Run Code Online (Sandbox Code Playgroud)

响应正文 -

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <m:CapitalCityResponse xmlns:m="http://www.oorsprong.org/websamples.countryinfo">
            <m:CapitalCityResult>New Delhi</m:CapitalCityResult>
        </m:CapitalCityResponse>
    </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

现在我无法弄清楚如何在我的 NestJs 虚拟应用程序中使用此服务。经过一段时间的探索,我发现了一个 Nestjs-soap 库,我安装并配置了它,如下所述 -

npm install nestjs-soap --save
Run Code Online (Sandbox Code Playgroud)

我的 app.module.ts -

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SoapModule } from 'nestjs-soap';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true
    }),
    SoapModule.registerAsync([
      { name: 'MY_DUMMY_CLIENT', uri: `http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL` }
    ])
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

我的应用程序.service.ts -

import { Inject, Injectable } from '@nestjs/common';
import { Client } from 'nestjs-soap';

@Injectable()
export class AppService {

  constructor(@Inject('MY_DUMMY_CLIENT') private readonly myDummyClient: Client) {}

  getHello(): string {
    return process.env.APP_MESSAGE;
  }

  getDetails(): string {
    return process.env.APP_DETAILS;
  }

  asyncGetCountryCapital() {
    //this.myDummyClient.
  }
}
Run Code Online (Sandbox Code Playgroud)

由于我引用的文档不详细,我无法弄清楚如何在 asyncGetCountryCapital() 函数中调用 SOAP 服务。

RRG*_*T19 1

今天我需要做同样的事情,结果发现文档nestjs-soap不太清楚。

在尝试了一些事情之后,这对我有用。

1. 使用方法

这方面的文档在这里

不发送参数的示例

模块:

SoapModule.register({
    clientName: 'CountryInfoService',
    uri: 'http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?wsdl',
})
Run Code Online (Sandbox Code Playgroud)

控制器:

import { Client } from 'nestjs-soap';

constructor(
    @Inject('CountryInfoService') private readonly soapClient: Client,
) {}

@Get('soap')
soap() {
    return new Promise((resolve, reject) => {
        this.soapClient.ListOfContinentsByName(null, (err, res) => {
            if (res) {
                resolve(res);
            } else {
                reject(err);
            }
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

请注意第一个参数,我需要传递null(也可以{}),因为没有它,我无法在第二个参数中声明该函数。

另外,我已经将其封装起来new Promise(),以便能够将其返回给客户端。

发送参数示例

模块:

让我们使用另一个uri来实现此目的,这是我在那里找到的唯一一个。

SoapModule.register({
    clientName: 'NumberConversion',
    uri: 'https://www.dataaccess.com/webservicesserver/NumberConversion.wso?wsdl',
})
Run Code Online (Sandbox Code Playgroud)

控制器:

import { Client } from 'nestjs-soap';

constructor(
    @Inject('NumberConversion') private readonly soapClient: Client,
) {}

@Get('soap')
soap() {
    return new Promise((resolve, reject) => {
        this.soapClient.NumberToWords({ ubiNum: 235 }, (err, res) => {
            if (res) {
                resolve(res);
            } else {
                reject(err);
            }
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

注意第一个参数,您需要传递一个具有您需要的任何属性的对象。

2.使用methodAsync

这方面的文档在这里

不发送参数的示例

@Get('soap')
async soap() {
    return await this.soapClient.YourFunctionNameAsync(null);
}
Run Code Online (Sandbox Code Playgroud)

发送参数示例

@Get('soap')
async soap() {
    return await this.soapClient.YourFunctionNameAsync({a: 1, b: 'hi'});
}
Run Code Online (Sandbox Code Playgroud)

Async这里的关键是在函数名称的末尾添加该单词。例如,采用之前的函数ListOfContinentsByName,名称将为ListOfContinentsByNameAsync.

--

我仔细研究了Nodejs Soap库,得到了这个解决方案。关于如何使用它的第一个示例表明您需要一个client函数和一个method函数。

然后,查看了Nestjs-soap库的 GitHub 上的代码,我发现他们只提供了该client功能,而不是两者都提供。

您/我们需要提供该method功能,例如:

使用方法

// The second parameter as a function is the key
this.soapClient.YourFunctionName(null, (err, res) => {
});
Run Code Online (Sandbox Code Playgroud)

使用方法异步

// Add the word `Async` at the end of your function name
const result = await this.soapClient.YourFunctionNameAsync({a: 1, b: 'hi'});
Run Code Online (Sandbox Code Playgroud)

希望它有帮助。