This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Web Services and the fly likes Consuming a web service using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Consuming a web service using Java" Watch "Consuming a web service using Java" New topic
Author

Consuming a web service using Java

Shruti Sharma
Ranch Hand

Joined: Mar 25, 2010
Posts: 55
Hi,

I am completely new to web services. I have an assignment in which I have to consume a web service called SPIDeR through SOAP. I need to pass some data in an XML format and get the results back in an XML format. Is there a web tutorial wherein I can see how to consume web services in Java?

Please help.

Thanks,
-Shruti
Shiva.Om Kumar
Ranch Hand

Joined: Jun 14, 2010
Posts: 68
In simple words, you need to write program (Java Web Service), then you need to deploy it on server - this is called server-side (server - which serves the request)
you need to write a program which will call (consume) your other Java program (Java web service) - this is call client-side (which makes a request to server)

p l s try following;
http://java.sun.com/developer/technicalArticles/J2SE/jax_ws_2/

after reading, you can download & install netbeans
http://netbeans.org/downloads/
and create a sample Java web service.
Shruti Sharma
Ranch Hand

Joined: Mar 25, 2010
Posts: 55
Thank you so much for the reply!

I do not have to develop the web service. I just have to consume. SPIDeR is a web-based system. SPIDeR will return the results in an XML format. Also, there needs to be authentication/authorization done while making the web service call. How to pass the credentials while making a web service call?

Thanks,
-Shruti
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Shruthi,
This link might help.


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Shruti Sharma
Ranch Hand

Joined: Mar 25, 2010
Posts: 55
Thanks, Naren!

I have one more question. The web service link I have is:
https://webapp.dss.state.va.us/SPIDERWeb/services/SearchMainWService?wsdl

Can I try directly accessing it (by pasting it in browser) or do I have to always call this link from the java code?

When I paste it directly in browser, it gives me a certificate error.

Thanks,
-Shruti
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
That is a URI for the WSDL file for the web service. It is not the URI for the actual executable web service.

Web services are for two applications to communicate.

Humans can typically read a WSDL file in a browser for reference purposes, unless there are implemented security mechanisms to prevent this.
Shruti Sharma
Ranch Hand

Joined: Mar 25, 2010
Posts: 55
OK, so how do I access the actual web service URL? If I remove the ?wsdl from https://webapp.dss.state.va.us/SPIDERWeb/services/SearchMainWService?wsdl and then try accessing the URL, then I get a certificate error saying 'Mismatched Address. The security certificate presented by this website was issued for a different website's address. This problem might indicate an attempt to fool you or intercept any data you send to the server.'

There should be a way to directly access this website as well through browser.

Here is how the wsdl looks:

*****************************

<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="http://webservices.spider.dss.org" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://webservices.spider.dss.org" xmlns:intf="http://webservices.spider.dss.org" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types />
- <wsdl:message name="executeSearchResponse">
<wsdl:part name="executeSearchReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeSSNSearchRequest">
<wsdl:part name="executeSSNSearch" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeCaseNoSearchResponse">
<wsdl:part name="executeCaseNoSearchReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeSSNSearchResponse">
<wsdl:part name="executeSSNSearchReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeSearchRequest">
<wsdl:part name="executeSearch" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeClientIDSearchResponse">
<wsdl:part name="executeClientIDSearchReturn" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeCaseNoSearchRequest">
<wsdl:part name="executeCaseNoSearch" type="xsd:string" />
</wsdl:message>
- <wsdl:message name="executeClientIDSearchRequest">
<wsdl:part name="executeClientIDSearch" type="xsd:string" />
</wsdl:message>
- <wsdl:portType name="SearchMainWService">
- <wsdl:operation name="executeSearch" parameterOrder="executeSearch">
<wsdl:input message="impl:executeSearchRequest" name="executeSearchRequest" />
<wsdl:output message="impl:executeSearchResponse" name="executeSearchResponse" />
</wsdl:operation>
- <wsdl:operation name="executeSSNSearch" parameterOrder="executeSSNSearch">
<wsdl:input message="impl:executeSSNSearchRequest" name="executeSSNSearchRequest" />
<wsdl:output message="impl:executeSSNSearchResponse" name="executeSSNSearchResponse" />
</wsdl:operation>
- <wsdl:operation name="executeClientIDSearch" parameterOrder="executeClientIDSearch">
<wsdl:input message="impl:executeClientIDSearchRequest" name="executeClientIDSearchRequest" />
<wsdl:output message="impl:executeClientIDSearchResponse" name="executeClientIDSearchResponse" />
</wsdl:operation>
- <wsdl:operation name="executeCaseNoSearch" parameterOrder="executeCaseNoSearch">
<wsdl:input message="impl:executeCaseNoSearchRequest" name="executeCaseNoSearchRequest" />
<wsdl:output message="impl:executeCaseNoSearchResponse" name="executeCaseNoSearchResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="SearchMainWServiceSoapBinding" type="impl:SearchMainWService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="executeSearch">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="executeSearchRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:input>
- <wsdl:output name="executeSearchResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="executeSSNSearch">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="executeSSNSearchRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:input>
- <wsdl:output name="executeSSNSearchResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="executeClientIDSearch">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="executeClientIDSearchRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:input>
- <wsdl:output name="executeClientIDSearchResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="executeCaseNoSearch">
<wsdlsoap:operation soapAction="" />
- <wsdl:input name="executeCaseNoSearchRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:input>
- <wsdl:output name="executeCaseNoSearchResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservices.spider.dss.org" use="encoded" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="SearchMainWServiceService">
- <wsdl:port binding="impl:SearchMainWServiceSoapBinding" name="SearchMainWService">
<wsdlsoap:address location="https://surya9.dss.state.va.us/SPIDERWeb/services/SearchMainWService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
*******************************************

Please give me some ideas on it.

Thanks,
-Shruti

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Shruthi,
how do I access the actual web service URL?

The url for the web service is the end point address within WSDL. From your WSDL it is the location attribute

Usually, endpoint address followed by ?WSDL gives you WSDL of that web service but not always necessary. The WSDL can be elsewhere on the server and may have a different url.

Sometimes neither web service url nor its wsdl url can be directly accessed especially if they are available over secure transport protocol. Your application must prove it is a trusted caller through authentication or certificates. This is the reason why you couldn't access the urls directly in browser.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
OK, so how do I access the actual web service URL?


The details about using this service should be provided to authorized users from the owners of the service. This includes required security tokens, formats, and anything else needed (which only the authors know about.)

Since you are completely new to web services. Another answer to your question is, you access the web service URL by creating a properly written web service client application.


There should be a way to directly access this website as well through browser.


Not really. As I mentioned, a web service is for application-to-application communication. Humans entering a URL in a HTML-browser does not qualify as an "application." A web service client needs to be specifically created. Microsoft's Internet Explorer is not a web service client for example.

In regards to web service security, there are many, many options for implementing security. The publishers of a particular web service are typically the only source of security details about the service and how it is intented to be consumed.

A "web service" and a website are not the same thing.
Shruti Sharma
Ranch Hand

Joined: Mar 25, 2010
Posts: 55
Thanks a lot Naren for such an elaborate explanation.

I believe that I need to create client using wsimport from JAX-WS. I need to pass the data to web service in XML format and get it back in XML format. How do I pass the XML file to web service? Is there a quick tutorial on that?

Thanks a lot for the answers.

-Shruti
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
@Jimmy Clark:
The details about using this service should be provided to authorized users from the owners of the service

Humans can typically read a WSDL file in a browser for reference purposes, unless there are implemented security mechanisms to prevent this.

I am not sure why someone would create security mechanisms to prevent the viewing of a WSDL file. Especially since:
1) the intention of web services are to be located via public registries and
2) The WSDL just describes an interface. If the methods provided offer access to privileged operation some kind of runtime authentication would be created.
Could you please elaborate on your point and give a specific example? Because I have never thought/read of this before, i.e. restrict viewing of WSDL.
For example using annotations in java code WSDL is automatically generated and viewable by browser. Is the restriction of the viewing of WSDL configurable in the common case of annotating classes?

Thanks
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I am not sure why someone would create security mechanisms to prevent the viewing of a WSDL file. Especially since:
1) the intention of web services are to be located via public registries and


This is only partially accurate. There are many internal-only web service implementations alive in various commercial and government systems today.

To phrase your statement better, "the intention of some web services is to be used publicly."

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Jim Akmer wrote:I am not sure why someone would create security mechanisms to prevent the viewing of a WSDL file.

The less an attacker knows about what he's attacking, the harder is his task. That's why one suppresses unnecessary HTTP headers that give away the version of the server, for example.

the intention of web services are to be located via public registries and

No. Public registries have pretty much been a failure, not a single one worth mentioning remains in existence today. The only place where registries are still used are in some large companies for internal purposes.

Because I have never thought/read of this before, i.e. restrict viewing of WSDL.

With all due respect, then you haven't read much about security in the internet age. Start here: http://ws.apache.org/axis/java/security.html
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
@Lester,Jimmy: Thank you for your comments. I will look into this more as you say. One last question though. In java the most frequent way to work with web services is using annotations. The WSDL is automatically generated by these annotations. In this case, how can the viewing of WSDL be restricted?

@Lester:
Public registries have pretty much been a failure, not a single one worth mentioning remains in existence today.

So what is the current replacement for registries in order to locate web services?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Jim Akmer wrote:In java the most frequent way to work with web services is using annotations. The WSDL is automatically generated by these annotations. In this case, how can the viewing of WSDL be restricted?

That depends on the SOAP stack you're using. There may be a setting in the web.xml file of the underlying web app, or something similar. The documentation of the SOAP library in question should mention how to do it.

Jim Akmer wrote:
Public registries have pretty much been a failure, not a single one worth mentioning remains in existence today.

So what is the current replacement for registries in order to locate web services?


There isn't one. It was thought that a market place of publicly available commercial WS would emerge, and that registries would be used to locate one that fit your needs - but that never happened. These days, a company would make the WSDL available to authorized users so that you can build a client, but you'd have to come to know that the service exists by some other means (e.g., through a web search).
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Using Java annotations is one way to create SOAP-based web services. It is not the only way and is certainly not the best way. Either way, the creation of a WSDL file and the viewing of the file from a public browser are different things.

Web services are complex and there are still many aspects that have not matured, i.e. service registries.

In regards to public web services, many organizations do indeed make them available, e.g. Amazon.com, Ebay.com.
Gilbert Prashant Singh
Greenhorn

Joined: Nov 02, 2011
Posts: 5
This has been a real informative thread, especially on WS security. Thanks Jimmy & Lester.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Consuming a web service using Java
 
Similar Threads
use of XML in the webservices
currency conversion
Web services overall picture and Jax-RPC
calling web service with input soap xml file
What is Web Service?