One of the useful solutions is to use the JAX-WS API. To do this, I suppose that you have:
- IDE: Eclipse
- JDK: 6
- Tomcat 6 installed and configured to run under Eclipse, if it’s not the case, don’t be worry and click here to now how to do this.
- JAX-WS.jar downloaded in your machine, because the jars of this API are not all delivered with the JDK. If it is not the case, click here to do it.
To create a web service SOAP with JAX-WS, we should have the following components:
- The SOAP (Simple Object Access Protocol) as protocol to exchange XML data
- A WSDL file (Web Service Description Language) to describe how to communicate with the web service
- In the server side, a Java program should be deployed as a web service to use.
- A Java program client to consume the web service.
To illustrate this, we try to implement a simple web service that gets the name of the user as an Input parameter and guesses his lucky number as an Output result.
Let’s go:
- Create a dynamic web project under Eclipse, rename it luckyNumber, if you don’t know how to do this, click here and execute the steeps from 6. to 10. . The default output folder is build\classes
- Download the tow following files and past them in the com.workspace.luckynumber (to create) :
- LuckyNumber.java the principal bean of this application
- LuckyNumberService.java the java class implementing the web service
Many annotations are used:
- XMLElement : to sepecify the name element in the schema of the XML file that will be generated later
- @WebService : to specify the web service name
- @WebMethod : to specify the method name that will be used in the web service
- @Param : to specify the sent parameter name
- @Result : to specify the name of the result (Output of the web service)
- Create the SOAP envelope: run the console and go the default output folder to execute the following command: YOUR_WORKSPACE\luckyNumber\build\classes>wsgen -cp . com.workspace.luckynumber.LuckyNumberService -s ../../src
- As a result, two java classes should be generated in com.workspace.luckynumber.jaxws
- GetLuckyNumber
- GetLuckyNumberResponse
- To test this, you create a simple jsp page or you download the mine from here, and you put it in the webContent folder of your project.
- You have to configure your project and your web service, so download the following files and put them in the META-INF folder of your project (Right click > Save as…):
- web.xml : to configure the deployment of your project
- sun-jaxws.xml : to configure the deployment of your web service
- You remember the famous jax-ws.jar that I ask you to download it in the beginning? No. Ok, do it please by clicking here, unzip it and copy/paste all the jars in CATALINA_HOME/lib. At this step, you should have something like this
- Clean your project, deploy it in your tomcat server and start the server.
- To test this application click here or tape the following URL in your browser (http://localhost:8080/luckyNumber/)
Congratulations, you know now how to make a web service SAOP by using JAX-WS API.
See also :
- How to install a new tomcat server in your workspace?
- How to make a connection with a sqlServer database by using Java?
- How to make a connection with a mySql database by using Java?
- How to make a web service by using Eclipse?
4 comments
No ping yet
real estate Lewes de says:
October 3, 2011 at 2:40 am (UTC 0)
I consider something genuinely interesting about your site so I saved to bookmarks .
iphone 5 says:
August 6, 2011 at 10:11 am (UTC 0)
Excellent! I appreciate your blog post to this matter. It has been useful. my blog: how to make money online
car repos says:
August 5, 2011 at 1:52 pm (UTC 0)
I don’t even know how I got here, but I thought this post was pretty good. Cheers!
iphone 5 says:
August 4, 2011 at 10:55 am (UTC 0)
Good aftie, i am a blogger too. and i can see that you are a nice blogger too,