The Source for Java Technology Collaboration
User: Password:



Jitendra Kotamraju

Jitendra Kotamraju's Blog

Accessing Google Web Service using JAX-WS

Posted by jitu on January 28, 2006 at 10:57 PM | Comments (3)

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


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Hi ,

    I tried this sample with JAX-WS RI 2.0-b26-ea3 I got the following exception

    Exception in thread "main" Unable to create StAX reader or writer
    at com.sun.xml.ws.streaming.XMLStreamReaderFactory.createFreshXMLStreamReader(XMLStreamReaderFactory.
    ava:105)
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:140)
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parseWSDL(RuntimeWSDLParser.java:165)
    at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:62)
    at com.sun.xml.ws.wsdl.WSDLContext.(WSDLContext.java:57)
    at com.sun.xml.ws.client.ServiceContextBuilder.build(ServiceContextBuilder.java:77)
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:117)
    at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:50)

    Why am I getting this and how can circumvent this?

    Thanks in advance

    Posted by: narayanaa on July 18, 2006 at 06:59 AM

  • I tried the example with the doSpellingSuggestion.xml and I am getting the following exception: Caused by: unexpected XML tag. expected: {http://schemas.xmlsoap.org/soap/envelope/}Fault but found: detail at com.sun.xml.ws.streaming.XMLStreamReaderUtil.verifyTag(XMLStreamReaderUtil.java:202) at com.sun.xml.ws.encoding.soap.client.SOAPXMLDecoder.decodeFault(SOAPXMLDecoder.java:442) at com.sun.xml.ws.encoding.soap.client.SOAPXMLDecoder.decodeBody(SOAPXMLDecoder.java:169) at com.sun.xml.ws.encoding.soap.client.SOAPXMLDecoder.decodeEnvelope(SOAPXMLDecoder.java:142) at com.sun.xml.ws.encoding.soap.client.SOAPXMLDecoder.toInternalMessage(SOAPXMLDecoder.java:270)

    Posted by: alokojha on April 08, 2007 at 12:27 AM

  • Jitu, I have a question: in your doSpellingSuggestion.xml, where is the value of the namespace abbreviation "ns1:" defined? Or is its value irrelevant--you could omit the "ns1:" altogether if you wanted, or call it "nsApple:" or anything else if desired?

    Thanks,
    Glen

    Posted by: gmazza on October 29, 2007 at 03:42 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds