SOAP « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ SOAP ’

Generating stubs for SOAP calls using IntelliJ IDEA

Posted by Sachin on July 13th, 2010

Since working with SOAP calls in XML is very tiresome, we have libraries like axis and wsdl2java to generate stubs to make web-service client calls. I generated the stubs using IntelliJ IDEA to make SOAP calls in grails using the steps below.

1. Adding Framework Support

If the IntelliJ project does not already have the framework support, then add it by right clicking on the “ProjectName” project and selecting the “Add Framework Support”. Go to Web services client in left pane and select Apache Axis in the right pane. Also, specify the path where you want necessary files to be downloaded. It will prompt you to download files. Just click “Yes”.

2. Generate Java code from WSDL

Right click the project’s src/java directory, and select “Webservices >> Generate Java code from WSDL”. Now point to the URL and ensure that the “output path” points to “src/java” .

Using the generated stub, we should now be able to invoke web service calls by importing in the necessary classes where ever we want to use them.

No more haggling with XML.. cheers…!!!

~~With Regards~~

~~Sachin Anand~~

~~sachin@intelligrape.com~~

  • Share/Bookmark
Tags: , ,
Posted in Java tools

Working With Soap Calls

Posted by Sachin on June 8th, 2010

Hey,

SOAP is one of the popular ways of working with web-services (another being REST(another of my blog)), while working with any of the SOAP based API, you will get a WSDL (web services description/definition language) describing what all methods are supported by the web service. Its a rather complicated XML document.
A few days back I was working on consuming SOAP based web services. I could not use Groovy WS as the soap calls I needed to make were little complicated, So I used the old httpclient libraries to make the calls. Here is a very simple snippet of the code I was using.

import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.methods.*
class SoapcallController {
 
    def index = {
    def url = "https://www.paymentsample.biz/Gateway.asmx"
    def payload ='
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
xmlns:tns="http://tempuri.org/" 
xmlns:s="http://www.w3.org/2001/XMLSchema" 
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<SOAP-ENV:Body>
<tns:GetPrimaryPaymentSubTypes xmlns:tns="http://tempuri.org/">
<tns:paymentType>ACH</tns:paymentType>ACH</tns:GetPrimaryPaymentSubTypes>
</SOAP-ENV:Body></SOAP-ENV:Envelope>'
def method = new PostMethod(url)
    def client = new HttpClient()
 
    payload = payload.trim()
    method.addRequestHeader("Content-Type","text/xml")
    method.addRequestHeader("Accept","text/xml,application/xml;q=0.9")
    method.setRequestEntity(new StringRequestEntity(payload))
    def statusCode = client.executeMethod(method)
    println "STATUS CODE : ${statusCode}"
    def resultsString = method.getResponseBodyAsString()
    method.releaseConnection()
    //println new XmlSlurper().parseText(resultsString).toString()
    println resultsString
    }
}

In this code we are hitting the “url” with “payload” and getting the response back in the resultsString which itself will be an XML. The payload will change depending upon the WSDL provided.

To generate the payload XML using your own WSDL you can use http://www.soapclient.com/soaptest.html. Its a rather nice tool to use and play before using the web service in your application.

And of course rather than working with XML it is always better to work with objects and method calls,search for wsdl2java or better still Axis2 on google for generating classes from wsdl. I will put a blog on generating classes from WSDL using axis2 api in intelliJ IDEA shortly. :)

Sachin Anand
sachin@intelligrape.com
Intelligrape Software (P) Ltd.

  • Share/Bookmark
Tags: , ,
Posted in Groovy