Reputation:
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
Please take a look at my code and output below. Thanks
Code
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
public class AtomicNumber {
public static void main(String[] args) {
try {
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage smsg = mf.createMessage();
SOAPHeader shead = smsg.getSOAPHeader();
SOAPBody sbody = smsg.getSOAPBody();
shead.detachNode();
QName bodyName = new QName("http://www.webserviceX.NET", "GetAtomicNumber", "web");
SOAPBodyElement bodyElement = sbody.addBodyElement(bodyName);
QName qn = new QName("ElementName");
SOAPElement quotation = bodyElement.addChildElement(qn);
quotation.addTextNode("iron");
System.out.println("\n Soap Request:\n");
smsg.writeTo(System.out);
System.out.println();
URL endpoint = new URL("http://www.webservicex.net/periodictable.asmx");
SOAPMessage response = connection.call(smsg, endpoint);
System.out.println("\n Soap Response:\n");
System.out.println(response.getContentDescription());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
My Output
Soap Request:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><web:GetAtomicNumber xmlns:web="http://www.webserviceX.NET"><ElementName>sodium</ElementName></web:GetAtomicNumber></SOAP-ENV:Body></SOAP-ENV:Envelope>
Soap Response:
null
Update
This is what I am getting (Exception)
<faultcode>soap:Server</faultcode>
<faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: Procedure or function 'GetAtomicNumber' expects parameter '@ElementName', which was not supplied.
at WebServicex.periodictable.GetAtomicNumber(String ElementName)
--- End of inner exception stack trace ---</faultstring>
Upvotes: 2
Views: 21488
Reputation: 9
@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();
Upvotes: 0
Reputation: 17904
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>
Upvotes: 4
Reputation: 3116
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");
Upvotes: 1
Reputation: 2378
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!
Upvotes: 0
Reputation: 5745
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.
Upvotes: 0