- Joined
- Mar 1, 2007
- Messages
- 2
- Reaction score
- 0
Hi.
I'm trying to code web services but I have an error occured when I use SAAJ API while retrieving the SOAP response.
Can you help me ?
Here is my code :
Here is the console print :
Thanks.
I'm trying to code web services but I have an error occured when I use SAAJ API while retrieving the SOAP response.
Can you help me ?
Here is my code :
import javax.xml.soap.*;
import javax.xml.namespace.QName;
import java.util.Iterator;
import java.net.URL;
public class Request {
public static void main(String[] args) {
try {
//Getting a SOAPConnection Object
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
// Creating a message
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
// Add content to body
SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("http://localhost","additionner","myWS");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
// Delete "header"
header.detachNode();
// Adding content
QName childname1 = new QName("http://localhost","valeur1","myWS");
bodyElement.addChildElement(childname1).addTextNode("5");
QName childname2 = new QName("http://localhost","valeur2","myWS");
bodyElement.addChildElement(childname2).addTextNode("6");
message.writeTo(System.out);
System.out.println("\n");
// call method who takes two arguments :
// message : message being sent
// endpoint : destination to which the message should go
URL endpoint = new URL ("http://localhost:8080/testWS/services/CalculerWS");
SOAPMessage response = connection.call(message, endpoint);
// Close connection
connection.close();
//Getting the content of the message
SOAPBody soapBody = response.getSOAPBody();
Iterator iterator = soapBody.getChildElements(bodyName);
bodyElement = (SOAPBodyElement)iterator.next();
String add = bodyElement.getValue();
// Display the returned value
System.out.print("The return value of server: ");
System.out.println(add);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Here is the console print :
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<myWS:additionner xmlns:myWS="http://localhost">
<myWS:valeur1>5</myWS:valeur1>
<myWS:valeur2>6</myWS:valeur2>
</myWS:additionner>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
java.util.NoSuchElementException
at com.sun.xml.messaging.saaj.soap.impl.ElementImpl$3.next(ElementImpl.java:723)
at Request.main(Request.java:49)
Thanks.
Last edited: