How to create a web service SOAP?

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:

  1. 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
  2. Download the tow following files and past them in the com.workspace.luckynumber (to create) :

    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)
  3. 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
  4. As a result, two java classes should be generated in com.workspace.luckynumber.jaxws
    • GetLuckyNumber
    • GetLuckyNumberResponse
  5. 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.
  6. 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
  7. 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
  8. Clean your project, deploy it in your tomcat server and start the server.
  9. 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 :

Share it with your freinds on Facebook Tweet it to your freinds on Twitter Myspace Friendfeed Technorati del.icio.us Digg Google Yahoo Buzz StumbleUpon


No ping yet

  1. real estate Lewes de says:

    I consider something genuinely interesting about your site so I saved to bookmarks .

  2. iphone 5 says:

    Excellent! I appreciate your blog post to this matter. It has been useful. my blog: how to make money online

  3. car repos says:

    I don’t even know how I got here, but I thought this post was pretty good. Cheers!

  4. iphone 5 says:

    Good aftie, i am a blogger too. and i can see that you are a nice blogger too,   

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP SlimStat