File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes How to call web service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to call web service" Watch "How to call web service" New topic
Author

How to call web service

chetan padhye
Greenhorn

Joined: Apr 17, 2008
Posts: 15
Hi

I have generated following classes with jax-ws . How to call web service ... the way i found on some tutorial is not working ...

following classes i have ....

wsimport http://www.webservicex.net/geoipservice.asmx?wsdl

GeoIP.java
GeoIPService.java
GeoIPServiceSoap.java
GetGeoIP.java
GetGeoIPContext.java
GetGeoIPContextResponse.java
GetGeoIPResponse.java
ObjectFactory.java
package-info.java


Thanks in advance


Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
TellTheDetails
  • Show the code that didn't work as expected
  • How did the fact that the code wasn't working manifest itself? (e.g. any Exceptions)
  • The WSDL defines three bindings on the same endpoint, one SOAP, two HTTP bindings - which one are you trying to use?

  • Using the SOAP binding should look something like:

    GeoIPService service = new GeoIPService();
    GeoIPServiceSoap port = service.getGeoIPServiceSoapPort(); // Use the Soap binding

    /* prepare
    GetGeoIP request = new GetGeoIP();
    and populate it with data namely the IPAddress
    */
    GetGeoIPResponse response = port.GetGeoIP(request);
    GeoIP geoIP = response.GetGeoIPResult()
    /* geoIP contains ReturnCode, IP, ReturnCodeDetails, CountryName, ContryCode */
    chetan padhye
    Greenhorn

    Joined: Apr 17, 2008
    Posts: 15
    Hey


    Thank you very much dear it works (with soap binding )!

    .... what is difference between these hhtp ( get / post ) and soap bindings ... ?

    Which one should be used while writing ws client with pure jax-ws 2.0 only ... ?

    Thanks again ....


    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    You can see the difference by looking at

    http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP

    The SOAP binding exchanges SOAP envelopes in the HTTP request and response bodies.
    POST /geoipservice.asmx HTTP/1.1
    Host: www.webservicex.net
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: "http://www.webservicex.net/GetGeoIP"

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <GetGeoIP xmlns="http://www.webservicex.net">
    <IPAddress>string</IPAddress>
    </GetGeoIP>
    </soap:Body>
    </soap:Envelope>



    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <GetGeoIPResponse xmlns="http://www.webservicex.net">
    <GetGeoIPResult>
    <ReturnCode>int</ReturnCode>
    <IP>string</IP>
    <ReturnCodeDetails>string</ReturnCodeDetails>
    <CountryName>string</CountryName>
    <CountryCode>string</CountryCode>
    </GetGeoIPResult>
    </GetGeoIPResponse>
    </soap:Body>
    </soap:Envelope>


    The HTTP GET binding accepts the IPAddress as a query parameter and returns the information as raw XML in the HTTP response body.
    GET /geoipservice.asmx/GetGeoIP?IPAddress=string HTTP/1.1
    Host: www.webservicex.net



    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <GeoIP xmlns="http://www.webservicex.net">
    <ReturnCode>int</ReturnCode>
    <IP>string</IP>
    <ReturnCodeDetails>string</ReturnCodeDetails>
    <CountryName>string</CountryName>
    <CountryCode>string</CountryCode>
    </GeoIP>


    The HTTP POST binding accepts the url-encoded IPAddress as the body of the HTTP request and returns the information as raw XML in the HTTP response body.
    POST /geoipservice.asmx/GetGeoIP HTTP/1.1
    Host: www.webservicex.net
    Content-Type: application/x-www-form-urlencoded
    Content-Length: length

    IPAddress=string



    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <GeoIP xmlns="http://www.webservicex.net">
    <ReturnCode>int</ReturnCode>
    <IP>string</IP>
    <ReturnCodeDetails>string</ReturnCodeDetails>
    <CountryName>string</CountryName>
    <CountryCode>string</CountryCode>
    </GeoIP>


    The article RESTful Web Services describes how to access HTTP bindings using JAX-WS. Since the article was published JAX-WS has backed off the "RESTful" moniker. That part of the API is now simply referred to as "XML/HTTP Binding Support".

    Frankly using SOAP for this information exchange is absolute overkill.
    And in the presence of the HTTP GET binding using JAX-WS to obtain that information is absolute overkill.

    Given the existance of the HTTP GET binding you could just use a URLConnection or HttpClient with a URL like
    http://www.webservicex.net/geoipservice.asmx/GetGeoIP?IPAddress=72.21.210.250
    and then extract the desired information from the returned HTTP response body.
    chetan padhye
    Greenhorn

    Joined: Apr 17, 2008
    Posts: 15
    Hey thanks ,

    What pattern can be proper in order to call ws in my application with soap bindings .


    if i return port object from some class WebServicePort.getTestWsPort(); will that do or if i implement singleton pattern for giving port object will cause some concurrency issue in case multiple classes are using same port.....

    basically i m looking for some pattern my web application should have in order to have ws calls.

    Thanks
    Chetan ...

    public class WebServicePort {

    private static TestWsService tws;

    static {

    tws = new TestWsService();
    }

    public static TestWs getTestWsPort() {

    return tws.getTestWsPort();
    }

    }

    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    I'd stay away from the singleton approach - it's likely to cause trouble.

    Just create the port as you need it. While HTTP/1.1 supports (and defaults to) persistent connections they aren't as heavy weight as, for example, database connections. The "context" of related HTTP requests/response pairs isn't established through the HTTP connection but through other means like cookies and URL rewriting - which aren't relevant to an adjacent SOAP message sender and receiver.
    chetan padhye
    Greenhorn

    Joined: Apr 17, 2008
    Posts: 15
    Thanks for reply ,

    You mean in application business logic, when ever need WS I should create new W service instance and get port then create service request object and call it ....
    No any pattern needed ...this is going to incise ws and port objects as for i my app i dont have database ....every data will come and go via web service ...


    Your second part of http persistance connections aren't heavy ..I guess i dont get ...can you please elaborate on it ?


    Thanks
    Chetan

    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    chetan padhye wrote:Your second part of http persistance connections aren't heavy ..I guess i dont get ...can you please elaborate on it ?


    For example, database connections are relatively expensive to create and they tend to "contain" session data which makes them fairly heavy weight - this makes it worthwhile to create "connection pools" of database connections in order to improve performance by reusing and recycling existing database connections. Compared to database connections, HTTP connections are fairly light weight. There is no built-in "session" - all the necessary session information is sent with each request and each response. For the most part it is sufficient to assume that the connection only lasts for a single request/response pair. However to take advantage of a potential persistent HTTP connection it is advisable to place all request/response pairs that are related, that belong to a sequenced "conversation" over the same port/HttpClient. However giving multiple threads access to the same port and HTTP connection is most likely a bad idea.

    You mean in application business logic, when ever need WS I should create new W service instance and get port then create service request object and call it ....


    Not exactly. First of all you don't want your business logic to be dependent a specific web services technology or the classes that were generated by that technology. You also want to be able to test your business logic independently of the web service, just with a faux implementation of that service. So you need to design a "service interface" that provides the methods your business logic needs and that accepts and generates the classes that your business logic understands. Then you build a proxy/adapter/broker/fa├žade that implements that interface and internally creates and uses the WS port.

    This is basically the Business Delegate pattern. (Evolution of the J2EE Business Delegate Design Pattern).

    The business delegate would create a port (e.g. GeoIPServiceSoap - the "Business Service") the first time it needs it from the ServiceLocator (e.g. GeoIPService) and subsequently use it for every WS access. The business logic would create a business delegate and use it for an entire "conversation" - though business delegates shouldn't be shared between threads.


    this is going to include web service and port objects as my application doesn't have a database .... all the data will come and go via web service


    If web services are replacing your data sources then you should consider using Data Source Architectural Patterns to encapsulate the web service access code like:
    Data Mapper, Table Data Gateway (Endpoint Data Gateway?), Row Data Gateway (Document Data Gateway?), Active Record (Active Document?) and also the Repository (an Object-Relational (or in this case Object-Hierarchical) Metadata Mapping Pattern) with a web service strategy.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to call web service