Skip to main content

Accessing Google Web Service using JAX-WS

Posted by jitu on January 28, 2006 at 10:57 PM PST

Google has a rpc/enc Web Service for searching and fetching cached pages. There are many ways to access these services(for e.g. Google API). Can it be accessed using JAX-WS? JAX-WS doesn't support rpc/enc Web Services so you don't have nice typed objects to work with. But one can use javax.xml.ws.Dispatch to send and receive SOAP messages. The advantages of using JAXWS for this purpose is that you could use other JAX-WS infrastructure components like handlers. The following program is used to access the spelling suggestion service. The same program can be used to access other services as well by passing a different SOAP message.

                                                                                
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import java.net.URL;
import java.io.FileInputStream;
                                                                               
public class GoogleClient {
    public static void main(String[] args) throws Exception {
        String wsdl = "http://api.google.com/GoogleSearch.wsdl";
        URL url = new URL(wsdl);
        QName serviceName = new QName("urn:GoogleSearch", "GoogleSearchService");
        QName portName = new QName("urn:GoogleSearch", "GoogleSearchPort");
        Service service = Service.create(url, serviceName);
        Dispatch dispatch = service.createDispatch(portName,
            SOAPMessage.class, Service.Mode.MESSAGE);
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage req = mf.createMessage(null, new FileInputStream(args[0]));
        SOAPMessage res = dispatch.invoke(req);
        res.writeTo(System.out);
    }
}

You need to insert your own key in the file doSpellingSuggestion.xml. I am not going to give mine !

You can use mustang to compile and run to see the spelling suggestion.

$ java GoogleClient doSpellingSuggestion.xml