08 - SOAP Web Service example in Java: Page 5 of 5

Once created, we should see something similar to this:

          

Expand the returnPrice operation and let’s see the request that was created, it should be similar to this:

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:goj = "http://goj.demo.com">
   <soapenv:Header/>
   <soapenv:Body>
      <goj:returnPrice>
         <goj:productID>?</goj:productID>
      </goj:returnPrice>
   </soapenv:Body>
</soapenv:Envelope>

Now let’s change the value of <goj:productID> to 1. If you recall the code we set in the Demo java class, then it should return a price of 10. So, let’s send the request and check the response which should be similar to the following:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <returnPriceResponse xmlns="http://goj.demo.com">
         <returnPriceReturn>10.0</returnPriceReturn>
      </returnPriceResponse>
   </soapenv:Body>
</soapenv:Envelope>

If we set the value to something other than 1 or 2, we should get the -1 price, as the indicated in the code. Let’s try sending the following request with <goj:productID> set to 3

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:goj = "http://goj.demo.com">
   <soapenv:Header/>
   <soapenv:Body>
      <goj:returnPrice>
         <goj:productID>3</goj:productID>
      </goj:returnPrice>
   </soapenv:Body>
</soapenv:Envelope>

In the response we can now see that returnPriceReturn has a -1 as the value:

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd = "http://www.w3.org/2001/XMLSchema" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <returnPriceResponse xmlns = "http://goj.demo.com">
         <returnPriceReturn>-1.0</returnPriceReturn>
      </returnPriceResponse>
   </soapenv:Body>
</soapenv:Envelope>

Now let’s modify our example so we can see a fault element in our SOAP envelopes.

Go back to our source code.

Create a new Java class called ServiceException and make sure it has the following source code:

package com.demo.goj;
public class ServiceException extends Exception {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public ServiceException (){super();}
    public ServiceException (String message) { super (message);}
    public ServiceException (String message, Throwable cause) { super (message, cause); }
    public ServiceException (Throwable cause) { super (cause); }
}

 

Let’s modify our Demo.java class file so our returnPrice method returns an exception and not a “-1” when the productID does not match any product.

So let’s make the changes in the Demo.java file so it has the following code:

package com.demo.goj;
public class Demo {
    public String returnMessage (String name)
    {
        return "Hello " + name;
    }
    public double returnPrice (int productID) throws ServiceException
    {
        if (productID == 1)
            return 10;
        else if (productID == 2)
            return 20;
        else throw new ServiceException ("Product Not Found");
    }
}

Now let’s recreate our web service one more time, with the steps we saw before. Once we complete the recreation of the web service, we can review the WSDL file from the following link http://localhost:8080/SOAPDemo/services/Demo?wsdl=wsdl2.0 and it should have something similar to this:

<wsdl2:description xmlns:wsdl2 = "http://www.w3.org/ns/wsdl" xmlns:ns1 = "http://org.apache.axis2/xsd" xmlns:ns = "http://goj.demo.com" xmlns:wsaw = "http://www.w3.org/2006/05/addressing/wsdl" xmlns:wrpc = "http://www.w3.org/ns/wsdl/rpc" xmlns:wsoap = "http://www.w3.org/ns/wsdl/soap" xmlns:tns = "http://goj.demo.com" xmlns:wsdlx = "http://www.w3.org/ns/wsdl-extensions" xmlns:ax21 = "http://goj.demo.com/xsd" xmlns:xs = "http://www.w3.org/2001/XMLSchema" xmlns:whttp = "http://www.w3.org/ns/wsdl/http" targetNamespace = "http://goj.demo.com">
    <wsdl2:documentation>Please Type your service description here</wsdl2:documentation>
    <wsdl2:types>
        <xs:schema attributeFormDefault = "qualified" elementFormDefault = "qualified" targetNamespace = "http://goj.demo.com/xsd">
            <xs:complexType name = "ServiceException">
                <xs:sequence/>
            </xs:complexType>
        </xs:schema>
        <xs:schema xmlns:ax22 = "http://goj.demo.com/xsd" attributeFormDefault = "qualified" elementFormDefault = "qualified" targetNamespace = "http://goj.demo.com">
            <xs:import namespace = "http://goj.demo.com/xsd"/>
            <xs:element name = "DemoServiceException">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "ServiceException" nillable = "true" type = "ax21:ServiceException"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnPrice">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "productID" type = "xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnPriceResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "return" type = "xs:double"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnMessage">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "name" nillable = "true" type = "xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnMessageResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "return" nillable = "true" type = "xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl2:types>
    <wsdl2:interface name = "ServiceInterface">
        <wsdl2:fault name = "DemoServiceException" element = "ns:DemoServiceException"/>
        <wsdl2:operation name = "returnMessage" style = "http://www.w3.org/ns/wsdl/style/rpc" wrpc:signature = "name #in return #return " pattern = "http://www.w3.org/ns/wsdl/in-out">
            <wsdl2:input element = "ns:returnMessage" wsaw:Action = "urn:returnMessage"/>
            <wsdl2:output element = "ns:returnMessageResponse" wsaw:Action = "urn:returnMessageResponse"/>
        </wsdl2:operation>
        <wsdl2:operation name = "returnPrice" style = "http://www.w3.org/ns/wsdl/style/rpc" wrpc:signature = "productID #in return #return " pattern = "http://www.w3.org/ns/wsdl/in-out">
            <wsdl2:input element = "ns:returnPrice" wsaw:Action = "urn:returnPrice"/>
            <wsdl2:output element = "ns:returnPriceResponse" wsaw:Action = "urn:returnPriceResponse"/>
            <wsdl2:outfault ref = "tns:DemoServiceException" wsaw:Action = "urn:returnPriceDemoServiceException"/>
        </wsdl2:operation>
    </wsdl2:interface>
    <wsdl2:binding name = "DemoSoap11Binding" interface = "tns:ServiceInterface" type = "http://www.w3.org/ns/wsdl/soap" wsoap:version = "1.1">
        <wsdl2:fault ref = "tns:DemoServiceException"/>
        <wsdl2:operation ref = "tns:returnMessage" wsoap:action = "urn:returnMessage">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
        <wsdl2:operation ref = "tns:returnPrice" wsoap:action = "urn:returnPrice">
            <wsdl2:input/>
            <wsdl2:output/>
            <wsdl2:outfault ref = "tns:DemoServiceException"/>
        </wsdl2:operation>
    </wsdl2:binding>
    <wsdl2:binding name = "DemoHttpBinding" interface = "tns:ServiceInterface" whttp:methodDefault = "POST" type = "http://www.w3.org/ns/wsdl/http">
        <wsdl2:fault ref = "tns:DemoServiceException"/>
        <wsdl2:operation ref = "tns:returnMessage" whttp:location = "returnMessage">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
        <wsdl2:operation ref = "tns:returnPrice" whttp:location = "returnPrice">
            <wsdl2:input/>
            <wsdl2:output/>
            <wsdl2:outfault ref = "tns:DemoServiceException"/>
        </wsdl2:operation>
    </wsdl2:binding>
    <wsdl2:binding name = "DemoSoap12Binding" interface = "tns:ServiceInterface" type = "http://www.w3.org/ns/wsdl/soap" wsoap:version = "1.2">
        <wsdl2:fault ref = "tns:DemoServiceException"/>
        <wsdl2:operation ref = "tns:returnMessage" wsoap:action = "urn:returnMessage">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
        <wsdl2:operation ref = "tns:returnPrice" wsoap:action = "urn:returnPrice">
            <wsdl2:input/>
            <wsdl2:output/>
            <wsdl2:outfault ref = "tns:DemoServiceException"/>
        </wsdl2:operation>
    </wsdl2:binding>
    <wsdl2:service name = "Demo" interface = "tns:ServiceInterface">
        <wsdl2:endpoint name = "DemoHttpEndpoint" binding = "tns:DemoHttpBinding" address = "http://10.116.65.66:8080/SOAPDemo/services/Demo.DemoHttpEndpoint/"/>
        <wsdl2:endpoint name = "DemoHttpSoap11Endpoint" binding = "tns:DemoSoap11Binding" address = "http://10.116.65.66:8080/SOAPDemo/services/Demo.DemoHttpSoap11Endpoint/"/>
        <wsdl2:endpoint name = "DemoHttpSoap12Endpoint" binding = "tns:DemoSoap12Binding" address = "http://10.116.65.66:8080/SOAPDemo/services/Demo.DemoHttpSoap12Endpoint/"/>
    </wsdl2:service>
</wsdl2:description>

In this new WSDL file we can see that there are several new elements to define the exception message, but also there are new declarations for the DemoServiceException fault, both in the interface element and in the binding element.

Once again, in SOAP UI, let’s remove the existing Demo project and re-create it, with the previous address: http://localhost:8080/SOAPDemo/services/Demo?wsdl

Expand the returnPrice method and try sending the following request with <goj:productID> set to 3:

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:goj = "http://goj.demo.com">
   <soapenv:Header/>
   <soapenv:Body>
      <goj:returnPrice>
         <goj:productID>3</goj:productID>
      </goj:returnPrice>
   </soapenv:Body>
</soapenv:Envelope>

In the response we receive from our web service we should not see any more the -1 in the returnPrice now we should be receiving a fault from our web service indicating that the value we sent does not match any product in our system and a “Product not found” should be sent in the reason for the fault we sent.

<soapenv:Envelope xmlns:soapenv = "http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <soapenv:Fault>
         <soapenv:Code>
            <soapenv:Value>soapenv:Receiver</soapenv:Value>
         </soapenv:Code>
         <soapenv:Reason>
            <soapenv:Text xml:lang = "en-US">Product Not Found</soapenv:Text>
         </soapenv:Reason>
         <soapenv:Detail>
            <ns:DemoServiceException xmlns:ns = "http://goj.demo.com">
               <ServiceException xsi:type = "ax21:ServiceException" xmlns = "http://goj.demo.com" xmlns:ax21="http://goj.demo.com/xsd" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"/>
            </ns:DemoServiceException>
         </soapenv:Detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

With this we conclude our SOAP tutorial.

Like us on Facebook