I succeeded in calling a web service, from a web service. However, I'd really like
someone out there who knows what they are doing to show me how to do this correctly. I am
sure there is a better way than what I did.
This is how I got it to work:
1. Create web service B as a stateless session bean web service using wstools to create
artifacts (ejb 2.1). Deployed web service.
2. Create web service A as a stateless session bean using wscompile to generate web
service artifacts. Copied WSDL from web service B and used wscompile to create client
artifacts. However, I don't ever use the generated stub/service/serializer classes,
because when I do, I get a SOAP message with duplicate body/envelope tags. What I do is
create my own SOAP message using a SOAPMessageFactory. Then I added namespacese,
attributes and elements to the message so it complies with the WSDL. Next I create a
connection and call the web service. When the message returns I parse it, retrieve the
return value and pass it back to the client.
3. Create a web client that calls web service A. Web service A calls web service B,
which returns a value to A, which returns a value to the client.
So, web service A is both a server to the web client and a client to web service B. Web
service A recieves the SOAP message from the web client and immediately calls web service
B. Here is the code in web service A that calls web service B:
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connection = scf.createConnection();
MessageFactory msgFactory = MessageFactory.newInstance();
// Create a message
SOAPMessage msg = msgFactory.createMessage();
// Create an envelope in the message
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
// Bind ns0 namespace
envelope.addNamespaceDeclaration("ns0","http://aaa/bbb/1.0");
// Get hold of the the body
SOAPBody body = envelope.getBody();
body.addChildElement(envelope.createName("ns0:getGreeting", "",
""))
.addAttribute(envelope.createName("ns0"), "http://aaa/bbb/1.0");
URL endpoint = new URL("http://localhost:8082/ws/getGreeting");
msg.saveChanges();
// Make call to web service
SOAPMessage soapMessage = connection.call(msg,
"http://localhost:8082/ws/getGreeting");
soapMessage.writeTo(System.out);
// The return value is in the grandchild element to the body tag.
Iterator it = soapMessage.getSOAPBody().getChildElements();
SOAPElement child = it.next();
Iterator it2 = child.getChildElements();
SOAPElement result = it2.next();
responseMessage = result.getValue();
As I said, there has to be an easier way to do this. If someone could show me a simpler
way to call a web service from a web service, I'd appreciate it.
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4052309#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...