Creating and sending a SOAP message in java to a very simple web service

SomeDude

I am currently trying to create and send a SOAP message to a very simple web service I've created myself.

Obviously the webservice have a wsdl file, which you can see here:

wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://service.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://service.com">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.com">
<xs:element name="echoTest">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="echoString" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="echoTestResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="echoTestRequest">
<wsdl:part name="parameters" element="ns:echoTest"/>
</wsdl:message>
<wsdl:message name="echoTestResponse">
<wsdl:part name="parameters" element="ns:echoTestResponse"/>
</wsdl:message>
<wsdl:portType name="RequestHandlerPortType">
<wsdl:operation name="echoTest">
<wsdl:input message="ns:echoTestRequest" wsaw:Action="urn:echoTest"/>
<wsdl:output message="ns:echoTestResponse" wsaw:Action="urn:echoTestResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="RequestHandlerSoap11Binding" type="ns:RequestHandlerPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="echoTest">
<soap:operation soapAction="urn:echoTest" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="RequestHandlerSoap12Binding" type="ns:RequestHandlerPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="echoTest">
<soap12:operation soapAction="urn:echoTest" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="RequestHandlerHttpBinding" type="ns:RequestHandlerPortType">
<http:binding verb="POST"/>
<wsdl:operation name="echoTest">
<http:operation location="echoTest"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="RequestHandler">
<wsdl:port name="RequestHandlerHttpSoap11Endpoint" binding="ns:RequestHandlerSoap11Binding">
<soap:address location="http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler.RequestHandlerHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="RequestHandlerHttpSoap12Endpoint" binding="ns:RequestHandlerSoap12Binding">
<soap12:address location="http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler.RequestHandlerHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="RequestHandlerHttpEndpoint" binding="ns:RequestHandlerHttpBinding">
<http:address location="http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler.RequestHandlerHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

I've then used the google chrome plugin called Wizdler, to find out how the structure of soap messages should look like. THe Soap message for this example, should look like this.

<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope">
    <Body>
        <echoTest xmlns="http://service.com">
            <echoString>[string?]</echoString>
        </echoTest>
    </Body>
</Envelope>

So a pretty simple and straightforward SOAP message.. Nevertheless, Ican get it to work. I am trying to create the SOAP message with the following code:

        MessageFactory messageFactory = MessageFactory.newInstance();

        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://service.com";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        soapBody.addNamespaceDeclaration("", serverURI);
        SOAPElement soapBodyElem = soapBody.addChildElement("echoTest");

        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("echoString");
        soapBodyElem1.addTextNode("HELLO?!?");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("RequestHandler", serverURI);

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println();

The above code, produces the following SOAP message:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/>
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body xmlns="http://service.com">
      <echoTest xmlns="">
          <echoString xmlns="http://service.com">HELLO?!?</echoString> 
      </echoTest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I don't know why the echoString get the xmlns="http://service.com".. it should be in the echoTest tag instead.. and I guess this could be a part of the problem.

The response message looks like this:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body>
   <soapenv:Fault>
      <faultcode>soapenv:Server</faultcode>
      <faultstring>namespace mismatch require http://service.com found none</faultstring>
      <detail />
   </soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>

As can figure out, there is a problem with the namespace, but i seems like no matter what I do, the problem still occurs.

I hope someone can see the problem and maybe can provide a solution.

Any help will be greatly appreciated.

SomeDude

Anyways, after trying for a loooong time, I finally got it to work.. and of course just right after posting this question, I got it to work. So here is the complete java code for the problem described in the question:

public class PayCheckHandler {

    public PayCheckHandler() throws Exception {
        // TODO Auto-generated method stub

        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler?wsdl";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // print SOAP Response
        System.out.println();
        System.out.print("Response SOAP Message:");
        soapResponse.writeTo(System.out);
        soapConnection.close();

    }


    private static SOAPMessage createSOAPRequest() throws Exception {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();

            String serverURI = "http://service.com";

            // SOAP Envelope
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPHeader header = envelope.getHeader();
            SOAPBody body = envelope.getBody();


            // SOAP Body
            SOAPBodyElement element = body.addBodyElement(envelope.createName("echoTest", "", serverURI));
            element.addChildElement("echoString").addTextNode("Hello!!!");



            return soapMessage;
    }

    public static void main(String[] args) throws Exception {
        new PayCheckHandler();
    }

}

Btw. if I absolutely didn't have to create my own SOAP messages manually, I would do as artem.ponchakov suggested.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

runtime modeler error while creating very simple web service in java

From Dev

Sending SOAP message by Proxy in Java

From Dev

Very simple Web Service in Python

From Dev

Very simple Web Service in Python

From Dev

Sending a simple message from Service to Activity

From Dev

Manually calling a web service with a SOAP message

From Dev

Calling a web service SOAP in java

From Dev

sending custom header to SOAP web service with Axis 2

From Dev

C# soap web service and Java client

From Dev

Java- SOAP Web service over Https

From Dev

Compressing request and response soap message of wcf web service

From Dev

Compressing request and response soap message of wcf web service

From Dev

Sending SOAP request to a specific service

From Dev

Creating a very simple upstart script

From Dev

Creating a very simple upstart script

From Dev

Java web service not giving error message

From Dev

Trying to implement a simple JSON web service in Java

From Dev

How to decide on what framework to use in Java Web Service?(SOAP)

From Dev

SOAP Web service need to send request using https protocol in java

From Dev

Web service using php soap server and java client

From Dev

SOAP Web service need to send request using https protocol in java

From Dev

SSRS Render reports using JAVA and SOAP Web Service

From Dev

Soap web service not getting published for Custom Object return type in Java

From Dev

java class to trust all for sending file to https web service

From Dev

.NET Web-service - To SOAP or not to SOAP

From Dev

Blackboard SOAP web service php

From Dev

Consuming a SOAP web service with Swift

From Dev

Securing a PHP Web service (SOAP)

From Dev

SOAP Web Service with Inno Setup

Related Related

HotTag

Archive