首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用node-soap的简单webservice

使用node-soap的简单webservice
EN

Stack Overflow用户
提问于 2014-04-06 01:51:03
回答 2查看 15.7K关注 0票数 9

我正在尝试使用Node和node-soap使用SOAP实现一个简单的web服务,但是客户端在使用服务器时似乎遇到了问题。

代码语言:javascript
复制
assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: invalid message definition for document style binding

我的wsdl文件是:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="wscalc1"
                  targetNamespace="http://localhost:8000/wscalc1"
                  xmlns="http://localhost:8000/wscalc1"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

  <wsdl:message name="sumarRequest">
    <wsdl:part name="a" type="xs:string"></wsdl:part>
    <wsdl:part name="b" type="xs:string"></wsdl:part>
  </wsdl:message>

  <wsdl:message name="multiplicarRequest">
    <wsdl:part name="a" type="xs:string"></wsdl:part>
    <wsdl:part name="b" type="xs:string"></wsdl:part>
  </wsdl:message>

  <wsdl:message name="multiplicarResponse">
    <wsdl:part name="res" type="xs:string"></wsdl:part>
  </wsdl:message>

  <wsdl:message name="sumarResponse">
    <wsdl:part name="res" type="xs:string"></wsdl:part>
  </wsdl:message>


  <wsdl:portType name="calcP">
    <wsdl:operation name="sumar">
      <wsdl:input message="sumarRequest"></wsdl:input>
      <wsdl:output message="sumarResponse"></wsdl:output>
    </wsdl:operation>

    <wsdl:operation name="multiplicar">
      <wsdl:input message="multiplicarRequest"></wsdl:input>
      <wsdl:output message="multiplicarResponse"></wsdl:output>
    </wsdl:operation>
  </wsdl:portType>

  <wsdl:binding name="calcB" type="calcP">
    <soap:binding style="document" 
                  transport="http://schemas.xmlsoap.org/soap/http"/>

    <wsdl:operation name="sumar">
      <soap:operation soapAction="sumar"/>
      <wsdl:input>
        <soap:body use="literal" 
                   encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" 
                   encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </wsdl:output>
    </wsdl:operation>

    <wsdl:operation name="multiplicar">
      <soap:operation soapAction="multiplicar"/>
      <wsdl:input>
        <soap:body use="literal" 
                   encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" 
                   encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="ws">
    <wsdl:port name="calc" binding="calcB">
      <soap:address location="http://localhost:8000/wscalc1"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

server.js

代码语言:javascript
复制
var soap = require('soap');
var http = require('http');

var service = {
    ws: {
        calc: {
            sumar : function(args) {
                var n = args.a + args.b;
                return { res : n };
            },

            multiplicar : function(args) {
                var n = args.a * args.b;
                return { res : n }
            }
        }
    }
}

var xml = require('fs').readFileSync('wscalc1.wsdl', 'utf8'),

server = http.createServer(function(request,response) {
    response.end("404: Not Found: "+request.url)
});

server.listen(8000);
soap.listen(server, '/wscalc1', service, xml);

client.js

代码语言:javascript
复制
var soap = require('soap');
var url = 'http://localhost:8000/wscalc1?wsdl';
soap.createClient(url, function(err, client) {

    if (err) throw err;


    console.log(client.describe().ws.calc);

    client.multiplicar({"a":"1","b":"2"},function(err,res){
        if (err) throw err;

        console.log(res);
    });
});

使用该代码,输出为:

代码语言:javascript
复制
{ sumar: 
   { input: { a1: 'Request', b1: 'Request' },
     output: { res: 'Response' } },
  multiplicar: 
   { input: { a2: 'Request', b2: 'Request' },
     output: { res: 'Response' } } }

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: invalid message definition for document style binding

任何帮助都将不胜感激

EN

回答 2

Stack Overflow用户

发布于 2016-03-10 04:17:36

正如user672320所指出的,client.js失败的原因是因为使用的wsdl格式是' RPC /literal‘,但是样式被设置为’文档‘而不是RPC。

事实上,五种格式中的任何一种都可以用于wsdl,每种格式都有不同的格式。

有关使用哪种样式的讨论,请参阅Which style of WSDL should I use?

此外,给出的示例也不完整。

扩展版本见下文:

wsdl文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="wscalc1" targetNamespace="http://localhost:8000/wscalc1" xmlns="http://localhost:8000/wscalc1" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <wsdl:message name="sumarRequest">
    <wsdl:part name="a" type="xs:string"/>
    <wsdl:part name="b" type="xs:string"/>
  </wsdl:message>
  <wsdl:message name="multiplicarRequest">
    <wsdl:part name="a" type="xs:string"/>
    <wsdl:part name="b" type="xs:string"/>
  </wsdl:message>
  <wsdl:message name="multiplicarResponse">
    <wsdl:part name="mulres" type="xs:string"/>
  </wsdl:message>
  <wsdl:message name="sumarResponse">
    <wsdl:part name="sumres" type="xs:string"/>
  </wsdl:message>
  <wsdl:portType name="calcP">
    <wsdl:operation name="sumar">
      <wsdl:input message="sumarRequest"/>
      <wsdl:output message="sumarResponse"/>
    </wsdl:operation>
    <wsdl:operation name="multiplicar">
      <wsdl:input message="multiplicarRequest"/>
      <wsdl:output message="multiplicarResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="calcB" type="calcP">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sumar">
      <soap:operation soapAction="sumar"/>
      <wsdl:input>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="multiplicar">
      <soap:operation soapAction="multiplicar"/>
      <wsdl:input>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ws">
    <wsdl:port binding="calcB" name="calc">
      <soap:address location="http://localhost:8001/wscalc1"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

server.js:

代码语言:javascript
复制
/*jslint node: true */
"use strict";

var soap = require('soap');
var http = require('http');

var service = {
    ws: {
        calc: {
            sumar : function(args) {
                var n = 1*args.a + 1*args.b;
                return { sumres : n };
            },

            multiplicar : function(args) {
                var n = args.a * args.b;
                return { mulres : n };
            }
        }
    }
};

var xml = require('fs').readFileSync('wscalc1.wsdl', 'utf8');

var server = http.createServer(function(request,response) {
    response.end("404: Not Found: "+request.url);
});

server.listen(8001);
soap.listen(server, '/wscalc1', service, xml);

client.js:

代码语言:javascript
复制
var soap = require('soap');
var url = 'http://localhost:8001/wscalc1?wsdl';

soap.createClient(url, function(err, client) {
    if (err) throw err;
    console.log(client.describe().ws.calc);
    client.multiplicar({a: 4,b: 3},function(err,res){
        if (err) throw err;
        console.log(res);
    });
    client.sumar({a: 4,b: 3},function(err,res){
        if (err) throw err;
        console.log(res);
    });
});

这样,代码输出是:

代码语言:javascript
复制
   { sumar:
   { input: { a: 'xs:string', b: 'xs:string' },
     output: { sumres: 'xs:string' } },
  multiplicar:
   { input: { a: 'xs:string', b: 'xs:string' },
     output: { mulres: 'xs:string' } } }
   { mulres: '12' }
   { sumres: '7' }
票数 6
EN

Stack Overflow用户

发布于 2015-04-08 05:37:31

在wsdl文件中,我将样式从文档更改为rpc,尝试使用client.js获得更多响应。

我收到的是这个输出。

代码语言:javascript
复制
{ sumar: 
   { input: { a: 'xs:string', b: 'xs:string' },
     output: { res: 'xs:string' } },
  multiplicar: 
   { input: { a: 'xs:string', b: 'xs:string' },
     output: { res: 'xs:string' } } }
{ res: '2' }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22884513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档