使用PHP使用Java Web服务时获取NullPointerException

时间:2013-07-18 13:52:28

标签: php web-services jax-ws

尝试使用PHP使用Java Web服务时出现以下错误:java.lang.NullPointerException

我认为it boils down to passing the parameter correctly。但是,此链接在我的情况下不起作用(提供stdClass对象)。

这是WSDL的相关部分

...
<message name="queryByPartyId">
    <part name="parameters" element="tns:queryByPartyId"/>
</message>
...
<operation name="queryByPartyId">
    <soap:operation soapAction=""/>
    <input><soap:body use="literal"/></input>
    <output><soap:body use="literal"/></output>
    <fault name="ServiceFault"><soap:fault name="ServiceFault" use="literal"/></fault>
</operation>

相关的PHP代码:

//things I've tried

//$theResponse = $soapClient->queryByPartyId("12345");
//$theResponse = $soapClient->__soapCall("queryByPartyId", array("queryByPartyId"=>12345));
//$theResponse = $soapClient->__soapCall("queryByPartyId", array("12345"));


//$addRequest = new stdClass();
//$addRequest->queryByPartyId = 12345;
//$theResponse = $soapClient->queryByPartyId($addRequest);

$theResponse = $soapClient->queryByPartyId(array("queryByPartyId"=>12345));

var_dump($theResponse);

$theResponse的格式转储:

object(SoapFault)#9 (10) { 
    ["message":protected]=> string(30) "java.lang.NullPointerException" 
    ["string":"Exception":private]=> string(0) "" 
    ["code":protected]=> int(0) 
    ["file":protected]=> string(51) "/path/to/index.php" 
    ["line":protected]=> int(82) 
    ["trace":"Exception":private]=> array(2) { 
        [0]=> array(6) { 
            ["file"]=> string(51) "/path/to/index.php" 
            ["line"]=> int(82) 
            ["function"]=> string(6) "__call" 
            ["class"]=> string(10) "SoapClient" 
            ["type"]=> string(2) "->" 
            ["args"]=> array(2) { 
                [0]=> string(14) "queryByPartyId" 
                [1]=> array(1) { 
                    [0]=> array(1) { 
                        ["queryByPartyId"]=> int(12345) 
                    } 
                } 
            } 
        } 
        [1]=> array(6) { 
            ["file"]=> string(51) "/path/to/index.php" 
            ["line"]=> int(82) 
            ["function"]=> string(14) "queryByPartyId" 
            ["class"]=> string(10) "SoapClient" 
            ["type"]=> string(2) "->" 
            ["args"]=> array(1) { 
                [0]=> array(1) { 
                    ["queryByPartyId"]=> int(12345) 
                } 
            } 
        } 
    } 
    ["previous":"Exception":private]=> NULL 
    ["faultstring"]=> string(30) "java.lang.NullPointerException" 
    ["faultcode"]=> string(8) "S:Server" 
    ["detail"]=> object(stdClass)#6 (1) { 
        ["exception"]=> object(stdClass)#7 (1) { 
            ["stackTrace"]=> object(stdClass)#8 (1) { 
                ["frame"]=> array(40) { 
                    [0]=> string(0) "" 
                    [1]=> string(0) "" 
                    [2]=> string(0) "" 
                    [3]=> string(0) "" 
                    [4]=> string(0) "" 
                    [5]=> string(0) "" 
                    [6]=> string(0) "" 
                    [7]=> string(0) "" 
                    [8]=> string(0) "" 
                    [9]=> string(0) "" 
                    [10]=> string(0) "" 
                    [11]=> string(0) "" 
                    [12]=> string(0) "" 
                    [13]=> string(0) "" 
                    [14]=> string(0) "" 
                    [15]=> string(0) "" 
                    [16]=> string(0) "" 
                    [17]=> string(0) "" 
                    [18]=> string(0) "" 
                    [19]=> string(0) "" 
                    [20]=> string(0) "" 
                    [21]=> string(0) "" 
                    [22]=> string(0) "" 
                    [23]=> string(0) "" 
                    [24]=> string(0) "" 
                    [25]=> string(0) "" 
                    [26]=> string(0) "" 
                    [27]=> string(0) "" 
                    [28]=> string(0) "" 
                    [29]=> string(0) "" 
                    [30]=> string(0) "" 
                    [31]=> string(0) ""
                    [33]=> string(0) ""
                    [35]=> string(0) "" 
                    [36]=> string(0) "" 
                    [37]=> string(0) "" 
                    [38]=> string(0) "" 
                    [39]=> string(0) "" 
                } 
            } 
        } 
    } 
} 

更新#1

WSDL评论:Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-

更新#2

在WSDL中找不到元素标记,但是存在模式位置。加载时,找到以下元素标记

<xs:complexType name="queryByPartyId">
    <xs:sequence>
        <xs:element name="queryByPartyIdInMsg" type="tns:queryByPartyIdInMessage" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="queryByPartyIdInMessage">
    <xs:sequence>
        <xs:element name="partyId" type="xs:string" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

更新#3

解决方案:

$addRequest = new stdClass();
$queryByPartyIdInMsg = new StdClass();

$queryByPartyIdInMsg->partyId="12345";
$addRequest->queryByPartyIdInMsg = $queryByPartyIdInMsg;

$theResponse = $soapClient->queryByPartyId($addRequest);

var_dump($theResponse);

找到元素结构后,the link mentioned previously actually helped to find the solution

1 个答案:

答案 0 :(得分:4)

再看看wsdl文件,在那里的某个地方,你会看到这样的东西:

<element name="queryByPartyId">
    <complexType>
        <element name="foobar" type="foo:int"/>
    </complexType>
</element>

这是服务在调用时希望您传递的内容的描述。内部elements具有nametype属性。要将其转换为通话,请执行以下操作:

$response = $soapClient->queryByPartyId(array('foobar'=> 123456));
var_dump($response);

wsdl 应该还包含服务将返回的描述:

<element name="queryByPartyIdResponse">
    <complexType>
        <element name="PartyReturn" type="xs:SomeType" />
    </complexType>
</element>

在此示例中,响应将是一个对象(或您的soapclient返回的任何对象),其中包含1个属性:PartyReturn,该属性的类型为SomeType。如果它是自定义类型(Java类),wsdl文件也将包含对它的描述。

在你的情况下:

$response = $soapClient->queryByPartyId(
    array('queryByPartyIdInMsg' => array('PartyId' => 123456))
);

或许您需要将queryByPartyIdInMsg子数组包装到另一个数组中,因为两个complexTypes的minOccurs都为0(表示数组)