[英]Getting NullPointerException when consuming Java web service using PHP
I'm getting the following error when attempting to consume a Java web service using PHP: java.lang.NullPointerException
. 尝试使用PHP使用Java Web服务时出现以下错误:
java.lang.NullPointerException
。
I think it boils down to passing the parameter correctly . 我认为归结为正确传递参数 。 However, this link didn't work in my case (providing an stdClass object).
但是,此链接在我的情况下不起作用(提供stdClass对象)。
Here is the relevant part of the WSDL 这是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>
Relevant PHP code: 相关的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);
The formatted dump of $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) ""
}
}
}
}
}
Update #1 更新#1
WSDL comment: Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-
WSDL注释:
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-
. Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.7-b01-
。
Update #2 更新#2
No element tags were found in the WSDL, however a schema location was present. 在WSDL中找不到元素标记,但是存在模式位置。 When loaded, the following element tag was found
加载时,找到以下元素标记
<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>
Update #3 更新#3
Solution: 解:
$addRequest = new stdClass();
$queryByPartyIdInMsg = new StdClass();
$queryByPartyIdInMsg->partyId="12345";
$addRequest->queryByPartyIdInMsg = $queryByPartyIdInMsg;
$theResponse = $soapClient->queryByPartyId($addRequest);
var_dump($theResponse);
Once you find the element structure, the link mentioned previously actually helped to find the solution . 一旦找到元素结构, 之前提到的链接实际上有助于找到解决方案 。
Take a second look at the wsdl file, somewhere in there, you'll see something like this: 再看看wsdl文件,在那里的某个地方,你会看到这样的东西:
<element name="queryByPartyId">
<complexType>
<element name="foobar" type="foo:int"/>
</complexType>
</element>
This is the description of what the service expects you to pass when calling it. 这是服务在调用时希望您传递的内容的描述。 the inner
elements
have name
and type
attributes. 内部
elements
具有name
和type
属性。 To translate that into a call, just do this: 要将其转换为通话,只需执行以下操作:
$response = $soapClient->queryByPartyId(array('foobar'=> 123456));
var_dump($response);
The wsdl should also contain a description of what the service will return: wsdl 还应包含服务将返回的内容的描述:
<element name="queryByPartyIdResponse">
<complexType>
<element name="PartyReturn" type="xs:SomeType" />
</complexType>
</element>
In this example, the response will be an object (or whatever your soapclient returns) with 1 property: PartyReturn
, that property will be of the type SomeType
. 在此示例中,响应将是一个对象(或者您的soapclient返回的任何对象),具有1个属性:
PartyReturn
,该属性将为SomeType
类型。 If it's a custom type (a Java class) the wsdl file will contain a description of that, too. 如果它是自定义类型(Java类),则wsdl文件也将包含对它的描述。
In your case: 在你的情况下:
$response = $soapClient->queryByPartyId(
array('queryByPartyIdInMsg' => array('PartyId' => 123456))
);
Or perhaps you'll need to wrap the queryByPartyIdInMsg
sub arrays into another array, because both complexTypes have a minOccurs of 0 (suggesting an array) 或者您可能需要将
queryByPartyIdInMsg
子数组包装到另一个数组中,因为两个complexTypes的minOccurs都为0(建议一个数组)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.