Hello,
When I test the web service by using the code that generated by the
wsdl2java tools provided in axis2, it just raised
NullPointerException:
java.lang.NullPointerException
at
org.apache.axis2.databinding.utils.ConverterUtil.convertToString(ConverterUtil.java:195)
at
test.client.TestservicePortStub$EchoRequest.getPullParser(TestservicePortStub.java:264)
at
test.client.TestservicePortStub.toEnvelope(TestservicePortStub.java:353)
at test.client.TestservicePortStub.echo(TestservicePortStub.java:90)
at test.client.Client1.main(Client1.java:41)
But if I followed the client coding in the userguide sample, it works fine.
Is there any problem in the code generator ?
The wsdl.
<wsdl:definitions xmlns:ns1="http://org.apache.axis2/xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://org.apache.axis2/"
targetNamespace="http://org.apache.axis2/"><wsdl:types><xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ns1="http://org.apache.axis2/xsd"
targetNamespace="http://org.apache.axis2/xsd"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="echoRequest">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:anyType" name="element" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="echoResponse">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:anyType" name="return" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema></wsdl:types><wsdl:message
name="echoResponseMessage"><wsdl:part name="part1"
element="ns1:echoResponse" /></wsdl:message><wsdl:message
name="echoRequestMessage"><wsdl:part name="part1"
element="ns1:echoRequest" /></wsdl:message><wsdl:portType
name="testservicePort"><wsdl:operation name="echo"><wsdl:input
message="tns:echoRequestMessage" /><wsdl:output
message="tns:echoResponseMessage"
/></wsdl:operation></wsdl:portType><wsdl:binding
name="testserviceBinding" type="tns:testservicePort"><soap:binding
transport="http://schemas.xmlsoap.org/soap/http" style="document"
/><wsdl:operation name="echo"><soap:operation name="operation"
soapAction="echo" style="document" /><wsdl:input><soap:body
use="literal" namespace="http://www.org.apache.axis2"
/></wsdl:input><wsdl:output><soap:body use="literal"
namespace="http://www.org.apache.axis2"
/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service
name="testservice"><wsdl:port name="testservicePortType"
binding="tns:testserviceBinding"><soap:address
location="http://localhost/axis2/services/testservice"
/></wsdl:port></wsdl:service></wsdl:definitions>
The client code:
/**
* Client1.java
*
* Copyright (C) 2006 Eric Chow. All right Reserved.
*
* This software is the confidential and proprietary information of Eric Chow.
*/
package test.client;
import org.apache.axis2.databinding.ADBBean;
/**
* Client1.java
*
* @author Chao Hoi Ka, Eric
*
*/
public class Client1 {
/**
*
*/
public Client1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
try {
TestservicePortStub stub = new TestservicePortStub();
TestservicePortStub.EchoRequest req =
(TestservicePortStub.EchoRequest)
getTestObject(TestservicePortStub.EchoRequest.class);
System.out.println(stub.echo(req));
} catch (Exception e) {
e.printStackTrace();
}
}
public static ADBBean getTestObject(Class type) throws Exception {
return (ADBBean) type.newInstance();
}
}
The code that generate from wsdl2java
/**
* TestservicePortStub.java
*
* This file was auto-generated from WSDL
* by the Apache Axis2 version: 0.94 Jan 11, 2006 (08:01:58 LKT)
*/
package test.client;
/*
* TestservicePortStub java implementation
*/
public class TestservicePortStub extends org.apache.axis2.client.Stub {
// default axis home being null forces the system to pick up the mars
from
// the axis2 library
public static final String AXIS2_HOME = null;
protected static org.apache.axis2.description.AxisOperation[]
_operations;
static {
// creating the Service
_service = new org.apache.axis2.description.AxisService(
"TestservicePort");
// creating the operations
org.apache.axis2.description.AxisOperation __operation;
_operations = new
org.apache.axis2.description.OutInAxisOperation[1];
__operation = new
org.apache.axis2.description.OutInAxisOperation();
__operation.setName(new javax.xml.namespace.QName(
"http://org.apache.axis2/", "echo"));
_operations[0] = __operation;
_service.addOperation(__operation);
}
public TestservicePortStub(
org.apache.axis2.context.ConfigurationContext
configurationContext,
String targetEndpoint) throws java.lang.Exception {
_serviceClient = new org.apache.axis2.client.ServiceClient(
configurationContext, _service);
_serviceClient.getOptions().setTo(
new
org.apache.axis2.addressing.EndpointReference(
targetEndpoint));
}
/**
* Default Constructor
*/
public TestservicePortStub() throws java.lang.Exception {
this("http://localhost/axis2/services/testservice");
}
/**
* Constructor taking the traget endpoint
*/
public TestservicePortStub(String targetEndpoint)
throws java.lang.Exception {
this(new org.apache.axis2.context.ConfigurationContextFactory()
.createConfigurationContextFromFileSystem(AXIS2_HOME),
targetEndpoint);
}
/**
* Auto generated method signature
*
* @see test.services.TestservicePort#echo
* @param param0
*
*/
public TestservicePortStub.EchoResponse echo(
TestservicePortStub.EchoRequest param0)
throws java.rmi.RemoteException {
org.apache.axis2.client.OperationClient _operationClient =
_serviceClient
.createClient(_operations[0].getName());
_operationClient.getOptions().setSoapAction("echo");
_operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);
// create SOAP envelope with that payload
org.apache.axis2.soap.SOAPEnvelope env = null;
// Style is Doc.
env = toEnvelope(getFactory(_options.getSoapVersionURI()),
param0);
// create message context with that soap envelope
org.apache.axis2.context.MessageContext _messageContext = new
org.apache.axis2.context.MessageContext();
_messageContext.setEnvelope(env);
// add the message contxt to the operation client
_operationClient.addMessageContext(_messageContext);
// set the options hierarchy
_options.setParent(_operationClient.getOptions());
_operationClient.setOptions(_options);
// execute the operation client
_operationClient.execute(true);
org.apache.axis2.context.MessageContext _returnMessageContext =
_operationClient
.getMessageContext(org.apache.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);
org.apache.axis2.soap.SOAPEnvelope _returnEnv =
_returnMessageContext
.getEnvelope();
java.lang.Object object = fromOM(getElement(_returnEnv, "doc"),
TestservicePortStub.EchoResponse.class);
return (TestservicePortStub.EchoResponse) object;
}
// http://localhost/axis2/services/testservice
public static class EchoResponse implements
org.apache.axis2.databinding.ADBBean {
public static final javax.xml.namespace.QName MY_QNAME = new
javax.xml.namespace.QName(
"http://org.apache.axis2/xsd", "echoResponse",
"ns1");
/**
* field for _return
*/
private org.apache.axis2.om.OMElement local_return;
/**
* Auto generated getter method
*
* @return org.apache.axis2.om.OMElement
*/
public org.apache.axis2.om.OMElement get_return() {
return local_return;
}
/**
* Auto generated setter method
*
* @param param
* _return
*/
public void set_return(org.apache.axis2.om.OMElement param) {
this.local_return = param;
}
/**
* databinding method to get an XML representation of this
object
*
*/
public javax.xml.stream.XMLStreamReader getPullParser(
javax.xml.namespace.QName qName) {
java.util.ArrayList elementList = new
java.util.ArrayList();
java.util.ArrayList attribList = new
java.util.ArrayList();
elementList.add(new javax.xml.namespace.QName(
"http://org.apache.axis2/xsd",
"return"));
elementList.add(org.apache.axis2.databinding.utils.ConverterUtil
.convertToString(local_return));
return org.apache.axis2.databinding.utils.ADBPullParser
.createPullParser(qName,
elementList.toArray(), attribList
.toArray());
}
/**
* static method to create the object Note - This is not
complete
*/
public static EchoResponse
parse(javax.xml.stream.XMLStreamReader reader)
throws java.lang.Exception {
EchoResponse object = new EchoResponse();
try {
int event = reader.getEventType();
int count = 0;
int argumentCount = 1;
boolean done = false;
// event better be a START_ELEMENT. if not we
should go up to
// the start element here
while (!reader.isStartElement()) {
event = reader.next();
}
while (!done) {
if
(javax.xml.stream.XMLStreamConstants.START_ELEMENT == event) {
if
("return".equals(reader.getLocalName())) {
String content =
reader.getElementText();
object
.set_return(org.apache.axis2.databinding.utils.ConverterUtil
.convertToanyType(content));
count++;
}
}
if (argumentCount == count) {
done = true;
}
if (!done) {
event = reader.next();
}
}
} catch (javax.xml.stream.XMLStreamException e) {
throw new java.lang.Exception(e);
}
return object;
}
}
public static class EchoRequest implements
org.apache.axis2.databinding.ADBBean {
public static final javax.xml.namespace.QName MY_QNAME = new
javax.xml.namespace.QName(
"http://org.apache.axis2/xsd", "echoRequest",
"ns1");
/**
* field for Element
*/
private org.apache.axis2.om.OMElement localElement;
/**
* Auto generated getter method
*
* @return org.apache.axis2.om.OMElement
*/
public org.apache.axis2.om.OMElement getElement() {
return localElement;
}
/**
* Auto generated setter method
*
* @param param
* Element
*/
public void setElement(org.apache.axis2.om.OMElement param) {
this.localElement = param;
}
/**
* databinding method to get an XML representation of this
object
*
*/
public javax.xml.stream.XMLStreamReader getPullParser(
javax.xml.namespace.QName qName) {
java.util.ArrayList elementList = new
java.util.ArrayList();
java.util.ArrayList attribList = new
java.util.ArrayList();
elementList.add(new javax.xml.namespace.QName(
"http://org.apache.axis2/xsd",
"element"));
elementList.add(org.apache.axis2.databinding.utils.ConverterUtil
.convertToString(localElement));
return org.apache.axis2.databinding.utils.ADBPullParser
.createPullParser(qName,
elementList.toArray(), attribList
.toArray());
}
/**
* static method to create the object Note - This is not
complete
*/
public static EchoRequest
parse(javax.xml.stream.XMLStreamReader reader)
throws java.lang.Exception {
EchoRequest object = new EchoRequest();
try {
int event = reader.getEventType();
int count = 0;
int argumentCount = 1;
boolean done = false;
// event better be a START_ELEMENT. if not we
should go up to
// the start element here
while (!reader.isStartElement()) {
event = reader.next();
}
while (!done) {
if
(javax.xml.stream.XMLStreamConstants.START_ELEMENT == event) {
if
("element".equals(reader.getLocalName())) {
String content =
reader.getElementText();
object
.setElement(org.apache.axis2.databinding.utils.ConverterUtil
.convertToanyType(content));
count++;
}
}
if (argumentCount == count) {
done = true;
}
if (!done) {
event = reader.next();
}
}
} catch (javax.xml.stream.XMLStreamException e) {
throw new java.lang.Exception(e);
}
return object;
}
}
private static javax.xml.namespace.QName[] qNameArray = {
};
private org.apache.axis2.om.OMElement toOM(
TestservicePortStub.EchoRequest param) {
if (param instanceof org.apache.axis2.databinding.ADBBean) {
org.apache.axis2.om.impl.llom.builder.StAXOMBuilder
builder = new
org.apache.axis2.om.impl.llom.builder.StAXOMBuilder(
org.apache.axis2.om.OMAbstractFactory.getOMFactory(),
param
.getPullParser(TestservicePortStub.EchoRequest.MY_QNAME));
org.apache.axis2.om.OMElement documentElement = builder
.getDocumentElement();
((org.apache.axis2.om.impl.OMNodeEx) documentElement)
.setParent(null); // remove the parent
link
return documentElement;
} else {
// todo finish this onece the bean serializer has the
necessary
// methods
return null;
}
}
private org.apache.axis2.soap.SOAPEnvelope toEnvelope(
org.apache.axis2.soap.SOAPFactory factory,
TestservicePortStub.EchoRequest param) {
if (param instanceof org.apache.axis2.databinding.ADBBean) {
org.apache.axis2.databinding.ADBSOAPModelBuilder
builder = new
org.apache.axis2.databinding.ADBSOAPModelBuilder(
param
.getPullParser(TestservicePortStub.EchoRequest.MY_QNAME),
factory);
return builder.getEnvelope();
} else {
// todo finish this onece the bean serializer has the
necessary
// methods
return null;
}
}
private org.apache.axis2.om.OMElement toOM(
TestservicePortStub.EchoResponse param) {
if (param instanceof org.apache.axis2.databinding.ADBBean) {
org.apache.axis2.om.impl.llom.builder.StAXOMBuilder
builder = new
org.apache.axis2.om.impl.llom.builder.StAXOMBuilder(
org.apache.axis2.om.OMAbstractFactory.getOMFactory(),
param
.getPullParser(TestservicePortStub.EchoResponse.MY_QNAME));
org.apache.axis2.om.OMElement documentElement = builder
.getDocumentElement();
((org.apache.axis2.om.impl.OMNodeEx) documentElement)
.setParent(null); // remove the parent
link
return documentElement;
} else {
// todo finish this onece the bean serializer has the
necessary
// methods
return null;
}
}
private org.apache.axis2.soap.SOAPEnvelope toEnvelope(
org.apache.axis2.soap.SOAPFactory factory,
TestservicePortStub.EchoResponse param) {
if (param instanceof org.apache.axis2.databinding.ADBBean) {
org.apache.axis2.databinding.ADBSOAPModelBuilder
builder = new
org.apache.axis2.databinding.ADBSOAPModelBuilder(
param
.getPullParser(TestservicePortStub.EchoResponse.MY_QNAME),
factory);
return builder.getEnvelope();
} else {
// todo finish this onece the bean serializer has the
necessary
// methods
return null;
}
}
private java.lang.Object fromOM(org.apache.axis2.om.OMElement param,
java.lang.Class type) {
Object obj;
try {
java.lang.reflect.Method parseMethod =
type.getMethod("parse",
new Class[] {
javax.xml.stream.XMLStreamReader.class });
obj = null;
if (parseMethod != null) {
obj = parseMethod.invoke(null, new Object[] {
param
.getXMLStreamReaderWithoutCaching() });
} else {
// oops! we don't know how to deal with this.
Perhaps the
// reflective one is a good choice here
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return obj;
}
}
Eric