users@glassfish.java.net

Invoking Web service using WSDL (JAX-WS)

From: <glassfish_at_javadesktop.org>
Date: Tue, 16 Jun 2009 10:54:47 PDT

I have developed this snippet that invoke the web service using WSDL. in response i am getting "null". can someone please help me.
thanks in advance

code, WSDL and schema file are as follow:
WSDL files
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. -->
<definitions targetNamespace="http://myservice.test.com/" name="TestServiceService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://myservice.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://myservice.test.com/" schemaLocation="TestServiceService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="GoalState">
    <part name="parameters" element="tns:GoalState"/>
  </message>
  <message name="GoalStateResponse">
    <part name="parameters" element="tns:GoalStateResponse"/>
  </message>
  <portType name="TestService">
    <operation name="GoalState">
      <input message="tns:GoalState"/>
      <output message="tns:GoalStateResponse"/>
    </operation>
  </portType>
  <binding name="TestServicePortBinding" type="tns:TestService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="GoalState">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="TestServiceService">
    <port name="TestServicePort" binding="tns:TestServicePortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>

Schema file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://myservice.test.com/" xmlns:tns="http://myservice.test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="GoalState" type="tns:GoalState"/>

  <xs:element name="GoalStateResponse" type="tns:GoalStateResponse"/>

  <xs:complexType name="GoalState">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="GoalStateResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

code is
main()
{
        URL wsdlLocation = new URL("http://localhost:8080/CalculatorService/TestServiceService?WSDL");
        
        
QName serviceName =
new QName("http://myservice.test.com/", "TestServiceService");
QName portName =
          new QName("http://myservice.test.com/", "TestServicePort");

        javax.xml.ws.Service service = null;
        try
        {
            service = javax.xml.ws.Service.create(wsdlLocation, serviceName);
        }
        catch(WebServiceException e)
        {
            System.out.print("error");
        }
        //Create a dispatch instance
        Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, javax.xml.ws.Service.Mode.MESSAGE);

                //get Binding provider
        BindingProvider bp = (BindingProvider) dispatch;

        // Configure RequestContext (optional step)
        Map<String, Object> rc = bp.getRequestContext();

        //Ask RequestContext to USE SOAPAction.
        rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);

        //Add SOAPAction to RequestContext
        rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, "GoalState");
        
                // Obtain a preconfigured SAAJ MessageFactory
        MessageFactory factory =
           ((javax.xml.ws.soap.SOAPBinding) bp.getBinding()).getMessageFactory();

        // Create SOAPMessage Request
        SOAPMessage request = factory.createMessage();

        // Request Header
        SOAPHeader header = request.getSOAPHeader();

        // Request Body
        SOAPBody body = request.getSOAPBody();

        // Compose the soap:Body payload
        QName payloadName =
           new QName("http://myservice.test.com/", "GoalState");

        SOAPBodyElement payload = body.addBodyElement(payloadName);

        SOAPElement message1 = payload.addChildElement("name");

        message1.addTextNode("farrukh");
        
        // Invoke the endpoint synchronously
        SOAPMessage reply = null;

        try {
                //Invoke Endpoint Operation and read response
                reply = dispatch.invoke(request);
        } catch (WebServiceException wse){
                wse.printStackTrace();
        }

        // process the reply
        body = reply.getSOAPBody();

        QName responseName =
           new QName("http://myservice.test.com/", "GoalStateResponse");

        SOAPBodyElement bodyElement = (SOAPBodyElement)body.getChildElements(responseName).next();
        String message = bodyElement.getValue();
}
}
[Message sent by forum member 's_mehdi76' (s_mehdi76)]

http://forums.java.net/jive/thread.jspa?messageID=351469