Thursday 16 April 2015

Create Web service client


In this post, I am going to explain how to create web service client that can talk to web service. For this example, I am going to use the GeoIPService web service.

GeoIPService is located at following location.

GeoIPService gives us the country name, by taking ip address as input.

Step 1: Create stub for the web service.

A stub for a remote object acts as a client's local representative or proxy for the remote object. The caller invokes a method on the local stub, which is responsible for carrying out the method call on the remote object.

When a stub's method is invoked, it does the following:
a. Initiates a connection with the remote JVM containing the remote object,
b. Marshals (writes and transmits) the parameters to the remote JVM,
c. Waits for the result of the method invocation,
d. Unmarshals (reads) the return value or exception returned, and returns the value to the caller.

We can create stub by using “wsimport” command and WSDL file, provided by the web service provider. “wsimport” comes with java SE installation itself.

To get the help for wsimport, open terminal/command prompt and type wsimport.

$ wsimport
Missing WSDL_URI


Usage: wsimport [options] <WSDL_URI>

where [options] include:
  -b <path>                 specify jaxws/jaxb binding files or additional schemas
                            (Each <path> must have its own -b)
  -B<jaxbOption>            Pass this option to JAXB schema compiler
  -catalog <file>           specify catalog file to resolve external entity references
                            supports TR9401, XCatalog, and OASIS XML Catalog format.
  -d <directory>            specify where to place generated output files
  -extension                allow vendor extensions - functionality not specified
                            by the specification.  Use of extensions may
                            result in applications that are not portable or
                            may not interoperate with other implementations
  -help                     display help
  -httpproxy:<host>:<port>  specify a HTTP proxy server (port defaults to 8080)
  -keep                     keep generated files
  -p <pkg>                  specifies the target package
  -quiet                    suppress wsimport output
  -s <directory>            specify where to place generated source files
  -target <version>         generate code as per the given JAXWS spec version
                            e.g. 2.0 will generate compliant code for JAXWS 2.0 spec
  -verbose                  output messages about what the compiler is doing
  -version                  print version information
  -wsdllocation <location>  @WebServiceClient.wsdlLocation value


Extensions:
  -XadditionalHeaders         map headers not bound to request or response message to 
                              Java method parameters
  -Xauthfile                  file to carry authorization information in the format 
                              http://username:password@example.org/stock?wsdl
  -Xdebug                     print debug information
  -Xno-addressing-databinding enable binding of W3C EndpointReferenceType to Java
  -Xnocompile                 do not compile generated Java files


Examples:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl

 
open the terminal,
a. Create  a directory “src” to store all the java files generated by wsimport.

$ pwd
/Users/harikrishna_gurram/study/webservices/examples
$ ls
src


b. Run wsimport command like below.


http://www.webservicex.net/geoipservice.asmx?WSDL” is the location of wsdl file. WSDL is an XML document that describes a web service. It shows which operations are available and how data should be structured to send to those operations.


$ wsimport -keep -s src http://www.webservicex.net/geoipservice.asmx?WSDL
parsing WSDL...


[WARNING] Ignoring SOAP port "GeoIPServiceSoap12": it uses non-standard SOAP 1.2 binding.
You must specify the "-extension" option to use this binding.
  line 197 of http://www.webservicex.net/geoipservice.asmx?WSDL

[WARNING] ignoring port "GeoIPServiceHttpGet": no SOAP address specified. try running wsimport with -extension switch.
  line 200 of http://www.webservicex.net/geoipservice.asmx?WSDL

[WARNING] ignoring port "GeoIPServiceHttpPost": no SOAP address specified. try running wsimport with -extension switch.
  line 203 of http://www.webservicex.net/geoipservice.asmx?WSDL

generating code...


compiling code...

 
It creates all the java sources files for the web service, compile and place the class files in proper directory structure.

$ pwd
/Users/harikrishna_gurram/study/webservices/examples
$ ls
net src
$ cd src
$ ls
net
$ cd net
$ ls
webservicex
$ cd webservicex/
$ ls
GeoIP.java   GeoIPServiceSoap.java  GetGeoIPContext.java  GetGeoIPResponse.java  package-info.java
GeoIPService.java  GetGeoIP.java   GetGeoIPContextResponse.java ObjectFactory.java
 
Step 2: Create new java project “webservices_tutorial” in eclipse.
Create two packages “net.webservicex” and “webservices.main”.

Copy all the java files created in step1 into net.webservicex package.

Create WebServiceClient.java in the package “webservices.main” package.


package webservices.main;

import net.webservicex.GeoIP;
import net.webservicex.GeoIPService;
import net.webservicex.GeoIPServiceSoap;

public class WebServiceClient {
 public static void main(String args[]){
  GeoIPService service = new GeoIPService();
  GeoIPServiceSoap geoIPServiceSoap = service.getGeoIPServiceSoap();
  GeoIP geoIP = geoIPServiceSoap.getGeoIP("1.1.1.1");
  System.out.println(geoIP.getCountryName());
  
  geoIP = geoIPServiceSoap.getGeoIP("2.2.2.2");
  System.out.println(geoIP.getCountryName());
  
  geoIP = geoIPServiceSoap.getGeoIP("3.3.3.3");
  System.out.println(geoIP.getCountryName());
  
  geoIP = geoIPServiceSoap.getGeoIP("4.4.4.4");
  System.out.println(geoIP.getCountryName());
 }
}


Total project structure looks like below.


Run WebServiceClient.java, you will get output like below.

Australia
France
United States
United States



Prevoius                                                 Next                                                 Home

1 comment:

  1. i'm getting this error

    Exception in thread "main" com.sun.xml.internal.ws.streaming.XMLStreamReaderException: XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,3]
    Message: The markup in the document preceding the root element must be well-formed.
    at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.wrapException(Unknown Source)
    at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.next(Unknown Source)
    at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.nextContent(Unknown Source)
    at com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil.nextElementContent(Unknown Source)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.hasWSDLDefinitions(Unknown Source)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
    at javax.xml.ws.Service.(Unknown Source)
    at net.webservicex.GeoIPService.(GeoIPService.java:41)
    at org.cloud.webservices.IPLocationFinder.main(IPLocationFinder.java:8)
    Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,3]
    Message: The markup in the document preceding the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
    at com.sun.xml.internal.ws.util.xml.XMLStreamReaderFilter.next(Unknown Source)
    ... 16 more

    ReplyDelete