I am very new to SOAP, so was looking into some programs online, this is what I came up with but I get a null response, must be some silly thing, but need little help
Pl
Do you really need to use bare SOAP classes? How about generating JAX-WS artifacts to get rid of all this boilerplate?
More info (incorporating ANT) here.
With cxf-codegen-plugin maven plugin you can create a client of the soap service quickly by adding these dependencies:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.4.2</version>
</dependency>
and adding this build section:
org.apache.cxf cxf-codegen-plugin 2.1.2 generate-sources generate-sources wsdl2java ${basedir}/target/generated-sources/cxf ${basedir}/src/main/resources/wsdlfile.wsdl -client -wsdlLocation -p com.package.for.generated.classes
then inside ${basedir}/target/generated-sources/cxf you will have classes required to call the web service and an example of how to do it.
Hope it helps!
Try with
QName qn = new QName("http://www.webserviceX.NET","ElementName","web");
EDIT: Also, as others have suggested - you will be better off using generated client code here - Axis, JAX-WS etc are all options.
The correct code should be as below.
QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber");
SOAPBodyElement bodyElement = sbody.addBodyElement(bodyName);
QName qn = new QName("ElementName");
What you want to do is auto-generate Java code for this web service. The WSDL is here: http://www.webservicex.net/periodictable.asmx?wsdl
In Java, the tool to auto-generate the code is wsimport
. You'll want to use something like this:
wsimport http://www.webservicex.net/periodictable.asmx?wsdl -p com.company.whateveruwant -Xnocompile -d . -keep
This will put the code you want in the specified package (here com.company.whateveruwant
).
From there, all you have to do is simply invoke the SOAP method like a normal Java library:
PeriodictableSoap soap = new Periodictable().getPeriodictableSoap();
System.out.println(soap.getAtomicNumber("Iron"));
This prints out:
<NewDataSet>
<Table>
<AtomicNumber>26</AtomicNumber>
<ElementName>Iron</ElementName>
<Symbol>Fe</Symbol>
<AtomicWeight>55.847</AtomicWeight>
<BoilingPoint>3300</BoilingPoint>
<IonisationPotential>7.9</IonisationPotential>
<EletroNegativity>1.6400000000000001</EletroNegativity>
<AtomicRadius>1.17</AtomicRadius>
<MeltingPoint>1808</MeltingPoint>
<Density>7874</Density>
</Table>
</NewDataSet>
@Ricky, this is the correct code.
SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
SOAPElement symbol = bodyElement.addChildElement("MyMetal");
symbol.addTextNode("iron");
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();
message.writeTo(System.out);
System.out.println();
response.writeTo(System.out);
System.out.println();