SOAP/SAAJ error

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 :

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:
Joined
Mar 1, 2007
Messages
2
Reaction score
0
Hello.

I've done a simple WebService wich add 2 numbers.
When I use SAAJ, I obtain an error.

Here il my SOAP 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();
}
}
}

And this is the error displays in the console :

java.util.NoSuchElementException
at com.sun.xml.messaging.saaj.soap.impl.ElementImpl$3.next(ElementImpl.java:723)
at Request.main(Request.java:49)[/code]

Have you got any idea to help me.

Thanks a lot.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top