aspose file tools*
The moose likes Web Services and the fly likes how to write java web service client 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 write java web service client " Watch "how to write java web service client " New topic
Author

how to write java web service client

ramalok naresh
Greenhorn

Joined: Oct 19, 2011
Posts: 1
Completely new bee in web services.

I am having the wsdl from some url abc.xyz.com/api/?wsdl(suppose) as:

<message name="call">
<part name="sessionId" type="xsd:string"/>
<part name="resourcePath" type="xsd:string"/>
<part name="args" type="xsd:anyType"/>
</message>

<message name="callResponse">
<part name="callReturn" type="xsd:anyType"/>
</message>

<portType name="Mage_Api_Model_Server_HandlerPortType">
<operation name="call">
<documentation>Call api functionality</documentation>
<input message="typens:call"/>
<output message="typens:callResponse"/>
</operation>
</portType >

<binding name="Mage_Api_Model_Server_HandlerBinding" type="typens:Mage_Api_Model_Server_HandlerPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="call">
<soapperation soapAction="urn:Mage_Api_Model_Server_HandlerAction"/>
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>

This is what I have tried:

Created a new JAVA project from Eclipse wizard.

right click on the project navigate to web service-> web service client.

Give the service definition http://abc.xyz.com/api/?wsdl(suppose)

in configuration:

(a) server runtime:tomcat v7.0 server
(b) webservice runtime:apache axis
Choose test client from slider (I have also tried develop client a bit confuse,as I have to create client much confused)

Click on finish.

As a result I found web service test client browser with some method, input and result. Also I got some JAVA file in my resources folder:

Mage_Api_Model_Server_HandlerBindingStub
Mage_Api_Model_Server_HandlerPortType
Mage_Api_Model_Server_HandlerPortTypeProxy
MagentoService
MagentoServiceLocator

now how to write my client code in java some thing like this just a try
:
thanks
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
If I read correctly,

1. From the wsdl, the operation you look for is call
2. From the interface generated Mage_Api_Model_Server_HandlerPortType you should see the call method with required parameters.

3. How:
MagentoServiceLocator locator = new MagentoServiceLocator();
Mage_Api_Model_Server_HandlerPortType port = locator.getMage_Api_Model_Server_HandlerPortType();

// to do: create the parameter
port.call (parameter);

4. Can you post the code for the class Mage_Api_Model_Server_HandlerPortType?
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi,

I am a newbie in web services stuffs. My main problem is how to use the URI link from my java program and invoke those methods provided by the webservices?

I would need some pointers developing web services client. The web services has already been deployed using Tomcat and Axis. I can see the exposed methods from the link. Now I need to write a client to be able to use those methods. Specifically, how to use the URI link from my java program and invoke those methods provided by the webservices?

I have followed the small tutorial below on eclipse on how to create a client.

http://px.pats.no/px/Eclipse_tutorial.html

However, I relaized that I do not have the following classes which I believe is used to access the web services.. In fact, I'm getting an error message.



I would really appreciate any help. Thanks in advance!

John
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
I do not have the IDE with me right now, so I can not be precise as step by step.

So roughly, Use Eclipse IDE:

1. create a dynamic web project
2. create a web service client using the WSDL URL http://px.pats.no/px/services/TerminalLocation?wsdl
2.1 choose server - Tomcat or whatever you are using
2.2 choose web service runtime - AXIS or AXIS1

This generate all the classes that you mentioned. These classes will allow you to call the web service provider.

If you have trouble at any given step, post the question.
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul and Naresh,

I managed to understand it. In fact I have my own service locator class. So, what I did was to create an object of mt class service locator.



Now, I know I can access the web services..Next question is can I directly call the method exposed by the web services ?
How can I proceeed?

Thanks in advance!



H Paul wrote:If I read correctly,

1. From the wsdl, the operation you look for is call
2. From the interface generated Mage_Api_Model_Server_HandlerPortType you should see the call method with required parameters.

3. How:
MagentoServiceLocator locator = new MagentoServiceLocator();
Mage_Api_Model_Server_HandlerPortType port = locator.getMage_Api_Model_Server_HandlerPortType();

// to do: create the parameter
port.call (parameter);

4. Can you post the code for the class Mage_Api_Model_Server_HandlerPortType?
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul and Naresh,

I managed to understand it. In fact I have my own service locator class. So, what I did was to create an object of mt class service locator.



Then, I call a function which return the url address of the web services and it returned the correct one.

Now, I know I can access the web services..Next question is can I directly call the method exposed by the web services ?
How can I proceed?

For example, if the web services has getName which receives a string name and returns also namses and family name having the name as input.

Thanks in advance!



H Paul wrote:I do not have the IDE with me right now, so I can not be precise as step by step.

So roughly, Use Eclipse IDE:

1. create a dynamic web project
2. create a web service client using the WSDL URL http://px.pats.no/px/services/TerminalLocation?wsdl
2.1 choose server - Tomcat or whatever you are using
2.2 choose web service runtime - AXIS or AXIS1

This generate all the classes that you mentioned. These classes will allow you to call the web service provider.

If you have trouble at any given step, post the question.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
If you look at the wsdl http://px.pats.no/px/services/TerminalLocation?wsdl
you will see the element portType. This is the interface that provide the methods that you/the client code can call


P.S: Usually, unless constraint by the old technology, use the latest and newest, in this case, use JAX-WS.
JAX-WS is web service version 2 where AXIS (JAX-RPC) is web service version 1.

Use GOOGLE: create web service with JAX-WS and Eclipse.
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Thanks a lot for your help.

I will try it out.


John
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

I have tried it and it may be working properly...I just need a confirmation if it is correct..

The object locator can be used to call a function that can return the URL address of the web services. In my case, it works.

The locator.getTerminalLocation() should return an object of type TerminalLocation.





Now, when I run my program I'm getting the following message. I have already define the username but maybe I need to define the password. But, I do not see any parameters for password. This is the reason why I'm asking the above question. When I deployed eclipse with why web service, I had already provided by username nad password..isn't that enough?



H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
If the web service provider require a basic auth, then

John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Work like magic thanks a lot!


Could you tell me what's happening on the following line?




Thanks
John
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. the port extends org.apache.axis.client.Stub
If you have any IDE, you can back track among the classes to the source, to Stub

2. Download http://javadoc.allimant.org/
Java Enterprise Edition 5 API Documentation

and see the package Package javax.xml.rpc for JAX-RPC webservice based ( webservice version 1) whereas JAX-WS webservice based, version 2

see
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

I may be off topic but I have created a web service client using Eclipse and it is working fine. I have written a java code to access all the methods provided by the web service. Now, I would like to deploy my java code(web service client) to a html page. Is it even possible? Do you have any suggestion so I could share or deploy the web service client ?

Thanks
John
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. Can we postpone your above question for few minutes?

2. Clarification: Are you trying to do:

A. Show a web page where the user can enter some data/field/values

(say requester, address, requestedAccuracy, acceptableAccuracy, maximumAge, responseTime, tolerance)

B. Upon submission, the server get the submitted data and re-use your web service client code
to call the web service provider and show to the user the result

(say location)

Is this correct?
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Thanks for your reply!

Yes, you are correct...just like in real situation.


Thanks
John

H Paul wrote:1. Can we postpone your above question for few minutes?

2. Clarification: Are you trying to do:

A. Show a web page where the user can enter some data/field/values

(say requester, address, requestedAccuracy, acceptableAccuracy, maximumAge, responseTime, tolerance)

B. Upon submission, the server get the submitted data and re-use your web service client code
to call the web service provider and show to the user the result

(say location)

Is this correct?
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
There are options...

Option 1:

1. Here is an example to set up a dynamic web site.
Have it set up and running to get familiar with Eclipse IDE way of creating a dynamic web site.

http://besthowtodo.com/blog/2010/05/how-to-create-dynamic-web-project-in-eclipse.html

2. Once step 1 is done, you'll know what do to for your requirement
in terms of creating new fields for data entry and submiting to the server
for the server (your ws client code) to use those entered data to call the web service provider.
For now, have step 1 done first.
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Thanks a lot! I will try it out and let you know.


Thanks
John

H Paul wrote:There are options...

Option 1:

1. Here is an example to set up a dynamic web site.
Have it set up and running to get familiar with Eclipse IDE way of creating a dynamic web site.

http://besthowtodo.com/blog/2010/05/how-to-create-dynamic-web-project-in-eclipse.html

2. Once step 1 is done, you'll know what do to for your requirement
in terms of creating new fields for data entry and submiting to the server
for the server (your ws client code) to use those entered data to call the web service provider.
For now, have step 1 done first.
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

I followed the procedure int he link below and I was able to make it work...after some time of troubleshooting.

I'm getting now the idea. In the process, I learned there were 3 files created which are .java, .jsp and .xml. So, The JSP serves as a html file and it takes the input from there. Then, the java class is somehow called and output the entered value.

However, I still don't see how I could used by deployed web service client because they belong to a different projects.


Thanks in advance for your help!



John



H Paul wrote:There are options...

Option 1:

1. Here is an example to set up a dynamic web site.
Have it set up and running to get familiar with Eclipse IDE way of creating a dynamic web site.

http://besthowtodo.com/blog/2010/05/how-to-create-dynamic-web-project-in-eclipse.html

2. Once step 1 is done, you'll know what do to for your requirement
in terms of creating new fields for data entry and submiting to the server
for the server (your ws client code) to use those entered data to call the web service provider.
For now, have step 1 done first.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
So

1. You have setup a JSP (view) where you have a form to allow the user to enter
(say requester, address, requestedAccuracy, acceptableAccuracy, maximumAge, responseTime, tolerance)
2. Once the user submit the form to the server, the Servlet (controller) take the inputs
from the user. Now, the Servlet (controller) will make use of the web service client code to call the web service provider.

Options:
-Deploy as 1 single WAR file: (on Tomcat web server for example.)
1. If you deploy the whole functionality in 1 war file, then you put the web service client code in a different package
in the same dynamic web project (war file)
OR
you can create a jar file that consist of web service client code and put it in the WEB-INF/lib folder of the
dynamic web project (war file)
By either way, the visibility is there for you to use web service client code.

-Deploy as 1 single EAR file: (consist of dynamic web project and the different web service client code project) (on any J2EE app server)
1. From the dynamic web project, you can put a reference to the project A (where you have web service client code)

By this way, the visibility is there for you to use web service client code from the different project.
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Thank you for the reply.

Based on my understanding, after reading the tutorial link you have provided, it is the Servlet(HelloDynaWebServelt.java) should be the one calling the methods from my web service client code, correct?

For now, I would most likely do the option below:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
you can create a jar file that consist of web service client code and put it in the WEB-INF/lib folder of the
dynamic web project (war file)
By either way, the visibility is there for you to use web service client code.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Question: All I have to do is create a .war out of my web service client code. Then put that file in WEB-INF/lib. After which, I can call or create object(s) of my web service client? or Do I need to import it within Servlet(HelloDynaWebServelt.java) as an example?

Sorry to ask but I'm kind of newbie...


Thanks
John


H Paul wrote:So

1. You have setup a JSP (view) where you have a form to allow the user to enter
(say requester, address, requestedAccuracy, acceptableAccuracy, maximumAge, responseTime, tolerance)
2. Once the user submit the form to the server, the Servlet (controller) take the inputs
from the user. Now, the Servlet (controller) will make use of the web service client code to call the web service provider.

Options:
-Deploy as 1 single WAR file: (on Tomcat web server for example.)
1. If you deploy the whole functionality in 1 war file, then you put the web service client code in a different package
in the same dynamic web project (war file)
OR
you can create a jar file that consist of web service client code and put it in the WEB-INF/lib folder of the
dynamic web project (war file)
By either way, the visibility is there for you to use web service client code.

-Deploy as 1 single EAR file: (consist of dynamic web project and the different web service client code project) (on any J2EE app server)
1. From the dynamic web project, you can put a reference to the project A (where you have web service client code)

By this way, the visibility is there for you to use web service client code from the different project.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. The servlet (controller) is using your WS client code to call WS provider: correct.

2. Two ways for re-use your WS client code:

A. Use the existing WS client code project that works,
add new JSP(view) and servlet (controller), then from the servlet re-use WS client code.
Now, every thing is 1 project and 1 deployable war file.

OR

B. Remember the steps - see posted Thursday, November 03, 2011 12:52:41 PM -
where you tried to generate AXIS client code, the IDE ask for the client project.
Now, try to do the same steps but this time, tell (or select) IDE to use the new Dynamic web site project.
In this way, you now have both WS client code and Dynamic web site code together in 1 project and deployed
as a war for Tomcat. Here you re-use the WS client code from the servlet (controller)
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

For now, I will try option A. However, when I try to create a servlet, IDE asked to provide a project name. If I create a new project, it will automatically create a new folder outside the project in which my web service clients are located. Maybe, I m missing somehing..I'm new in using eclipse IDE. Thanks in advance for your help.


Thanks
John





H Paul wrote:1. The servlet (controller) is using your WS client code to call WS provider: correct.

2. Two ways for re-use your WS client code:

A. Use the existing WS client code project that works,
add new JSP(view) and servlet (controller), then from the servlet re-use WS client code.
Now, every thing is 1 project and 1 deployable war file.

OR

B. Remember the steps - see posted Thursday, November 03, 2011 12:52:41 PM -
where you tried to generate AXIS client code, the IDE ask for the client project.
Now, try to do the same steps but this time, tell (or select) IDE to use the new Dynamic web site project.
In this way, you now have both WS client code and Dynamic web site code together in 1 project and deployed
as a war for Tomcat. Here you re-use the WS client code from the servlet (controller)
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
1. Select the Project where it has WS Client code - This is a Dynamic web project.

(see the post dated posted Thursday, November 03, 2011 12:52:41 PM)

2. Right click on the selected project and select New and select Servlet.Now you need to enter the package name and a servlet name.

This Step 2 is actually the same you follow in the link that I gave earlier post dated posted Saturday, December 10, 2011 8:31:19 AM
http://besthowtodo.com/blog/2010/05/how-to-create-...ic-web-project-in-eclipse.html
See picture with caption "Create Servlet"
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Thanks for the reply.


On November 3, I did not create a Dynamic web project. I just created a web service client project. I now created Dynamic project first and deploy my web service. I will work on it and keep you update.

Thank you for our patience.

Regards
John

H Paul wrote:1. Select the Project where it has WS Client code - This is a Dynamic web project.

(see the post dated posted Thursday, November 03, 2011 12:52:41 PM)

2. Right click on the selected project and select New and select Servlet.Now you need to enter the package name and a servlet name.

This Step 2 is actually the same you follow in the link that I gave earlier post dated posted Saturday, December 10, 2011 8:31:19 AM
http://besthowtodo.com/blog/2010/05/how-to-create-...ic-web-project-in-eclipse.html
See picture with caption "Create Servlet"
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Bit by bit I'm getting the idea. I did a small test..What I noticed is that my web service client needs to run on tomcat that is started as a windows service. However, the dynamic web project(servlet) needs to run as a shell mode. When I stopped Tomcat service(windows service), the web service will not be able to find the web service. If I start Tomcat service(windows service), the servlet will complain that the another service is using the same port. By the way, I use the same tomcat for the servlet and for my web servie deplyment.. Have you seen this issue before?


Thanks in advance!


Regards,
John


H Paul wrote:1. Select the Project where it has WS Client code - This is a Dynamic web project.

(see the post dated posted Thursday, November 03, 2011 12:52:41 PM)

2. Right click on the selected project and select New and select Servlet.Now you need to enter the package name and a servlet name.

This Step 2 is actually the same you follow in the link that I gave earlier post dated posted Saturday, December 10, 2011 8:31:19 AM
http://besthowtodo.com/blog/2010/05/how-to-create-...ic-web-project-in-eclipse.html
See picture with caption "Create Servlet"
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Just an update.. I have decided to deploy my web services on another tomcat. So for now, it is working.

Also, could you suggest a good tutorial for JSP? I know there's plenty out there but I rather use from someone who knows already JSP.

Thanks so much again!


John
John Santy wrote:Hi Paul,

Bit by bit I'm getting the idea. I did a small test..What I noticed is that my web service client needs to run on tomcat that is started as a windows service. However, the dynamic web project(servlet) needs to run as a shell mode. When I stopped Tomcat service(windows service), the web service will not be able to find the web service. If I start Tomcat service(windows service), the servlet will complain that the another service is using the same port. By the way, I use the same tomcat for the servlet and for my web servie deplyment.. Have you seen this issue before?


Thanks in advance!


Regards,
John


H Paul wrote:1. Select the Project where it has WS Client code - This is a Dynamic web project.

(see the post dated posted Thursday, November 03, 2011 12:52:41 PM)

2. Right click on the selected project and select New and select Servlet.Now you need to enter the package name and a servlet name.

This Step 2 is actually the same you follow in the link that I gave earlier post dated posted Saturday, December 10, 2011 8:31:19 AM
http://besthowtodo.com/blog/2010/05/how-to-create-...ic-web-project-in-eclipse.html
See picture with caption "Create Servlet"
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Good day!


I was able to create a Web Service client using eclipse. It works fine. Then, I can also now run all the .class from the command line(DOS) without using the eclipse interface.

My question is since all my generated .class are configured to use only one Web services http link(which was provided when I create my web service client from eclipse), how can implement so that I can provide http link of web service when running my java class. In this way, it is possible to use different web service link.

Thanks in advance for your help.


John.

H Paul wrote:1. Select the Project where it has WS Client code - This is a Dynamic web project.

(see the post dated posted Thursday, November 03, 2011 12:52:41 PM)

2. Right click on the selected project and select New and select Servlet.Now you need to enter the package name and a servlet name.

This Step 2 is actually the same you follow in the link that I gave earlier post dated posted Saturday, December 10, 2011 8:31:19 AM
http://besthowtodo.com/blog/2010/05/how-to-create-...ic-web-project-in-eclipse.html
See picture with caption "Create Servlet"
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
    
    4
Hi John,

1. I'm not sure if I understand what you asked.

2. So my guess: regardless of which way (eclipse/command line/or any other way), you can generate/create the client code from a given wsdl.

3. The given wsdl is deployed in a machine A, so your client code is pointing to machine A and worked.

4. Now if the same given wsdl is deployed into another machine B, your code is still working as it WITH only 1 change:
change the web service URL to point to the new machine B.




5. Now you have see how to do that with Axis API. Just Google, you should find an example.
John Santy
Greenhorn

Joined: Nov 03, 2011
Posts: 21
Hi Paul,

Thank you very much for your reply..

I managed to make it work.

H Paul wrote:Hi John,

1. I'm not sure if I understand what you asked.

2. So my guess: regardless of which way (eclipse/command line/or any other way), you can generate/create the client code from a given wsdl.

3. The given wsdl is deployed in a machine A, so your client code is pointing to machine A and worked.

4. Now if the same given wsdl is deployed into another machine B, your code is still working as it WITH only 1 change:
change the web service URL to point to the new machine B.




5. Now you have see how to do that with Axis API. Just Google, you should find an example.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to write java web service client