从JavaScript调用JAX-WSWeb服务时参数为Null

Null parameters when calling a JAX-WS Web Service from JavaScript

本文关键字:参数 Null 服务 JavaScript 调用 JAX-WSWeb      更新时间:2023-09-26

我有一个JAX-WS web服务,当它从任何客户端(即Java destkop应用程序)而不是从JavaScript调用时,它都能正常工作。

我的WS界面如下:

@WebService
public interface LicenseService {
    @WebMethod
    String getLicense(
            @WebParam(name="coupon") String coupon,
            @WebParam(name="licenseCode") String licenseCode,
            @WebParam(name="secret") String secret);
    }

我用这样的javascript调用它:

var request = new XMLHttpRequest();
request.open("POST", url, false);
request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
request.send(envelope);

并且发送的CCD_ 1看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <getLicense xmlns="http://ws.licenseman.elevelcbt.eu/">
            <coupon>SYcj1J9I</coupon>
            <licenseCode>BEPRO</licenseCode>
            <secret>1234567890</secret>
        </getLicense>
    </soap:Body>
</soap:Envelope>

该方法被调用(我可以在Java端跟踪),但所有传递的参数都为null。我的envelope格式/内容一定有问题。

好的,我明白了。我需要将我的envelope格式更改为这样(当我从java客户端成功调用WS时,我通过跟踪原始xml消息获得了它):

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <ns1:getLicense xmlns:ns1="http://ws.licenseman.elevelcbt.eu/">
            <coupon>1111</coupon>
            <licenseCode>BEPRO</licenseCode>
            <secret>xxxxxxx</secret>
        </ns1:getLicense>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

JAX-WS似乎不喜欢这样的方法声明:

<getLicense xmlns="http://ws.licenseman.elevelcbt.eu/">

并且想要这样:

<ns1:getLicense xmlns:ns1="http://ws.licenseman.elevelcbt.eu/">