Saturday, December 8, 2012

Create java web service in Eclipse using Axis2 (Part 02)

This is the second part of create java web service using Axis2 tutorial. In this post I'm going to demonstrate the configuration of Apache Axis2 in Eclipse and create a simple java web service using Apache Axis2 SOAP engine. (If you want to see the first part click here.)

1. First you need to download and extract Apache Axis2 SOAP engine.

 Apache Axis2 Binary Distribution - DOWNLOAD

2. Configure Axis2

In Eclipse go to Window >> Preferences >> Web Services >> Axis2 Preferences




 Then Browse to axis2 extracted location. If you configure correctly Eclipse will show this message "Axis2 runtime loaded successfully" then click on Apply button.


To set server & runtime in the same Preferences window click on Server & Runtime.



 Then select Tomcat 7 as Server runtime & Axis2 as Web service runtime.



To complete configurations finally click Apply and OK.

3. Creating the Dynamic web project
After completing those configurations we can directly jump in to the web service development part. Here I'm going to implement very simple project. You can change it as you want.

In Eclipse EE create new Dynamic web project. Give project name



- You have to select Tomcat 7 as Target runtime.
- Select 2.5 in Dynamic web module version drop down menu.
- Then click Modify... button appears just after the Configuration.
- I gave the "WebApp" as project name.


- Select Axis2 web services in opening window.


Click Finish to create the Dynamic web project.

Right click on src folder then New >> Class


- Give class name as well as package name
- I gave package name as "org.webapp.ws" and class name as "Hello"


- Then click finish to crate the class.

Add following code to the class


public class Hello {
 public String sayHello(String name) {
    return "Hello !!! " + name;
 }
}

4. Creating the web service

Right click on your service class (not the project).
Select Web Services >> Create Web Service



In Web Service window tick to the Monitor the Web service check box. Other default settings are OK. No need to change those to this web service.


Click Next.
Next window, select Generate a default services.xml file.



Then click Next and finally click Finish.

4. Testing the project
Perfect...... Now you should have a web service. To set the service Right click on your project and select Run As >> Run on Server.


Click next & click Finish.

If everything went fine you should see following page in Eclipse internal web browser.


Click here to download the project.
In my next post I hope to demonstrate the implementation of the client program.

If you find this post helpful don't forget to leave a comment. Your comments always encourage me to write more!

Thank you for watching.
Happy coding!

49 comments:

  1. thankyou...this site helped us a lot.

    ReplyDelete
  2. Really it's very useful to me..thanks .....keep it up..........

    ReplyDelete
  3. Thank you Chathura..
    nice tutorial for start.. :)

    ReplyDelete
  4. Nice one, it realy help me. Keep posting such nice things....!!!

    ReplyDelete
  5. Nice one, it realy help me. Keep posting such nice things....!!!

    ReplyDelete
  6. why is the project not running after doing run as>>Run on Server?????
    Please help Sir!

    ReplyDelete
  7. It is working for me! after doing Run As>>Run on Server, it is showing 404Error!
    How do i resolve it??
    It has succesfully started in my browser as well as eclipse browsser...but the web service fails to load!
    Please help! its urgent!

    ReplyDelete
    Replies
    1. Check the URL. If you testing this on same computer URL should follows this format http://localhost:PORT NUMBER/PROJECT NAME/

      Delete
    2. I gave this url http://localhost:8080/WebApp/

      And i even get this error in the console

      Dec 29, 2012 12:47:08 PM org.apache.catalina.core.AprLifecycleListener init
      INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre6/bin/client;C:/Program Files (x86)/Java/jre6/bin;C:/Program Files (x86)/Java/jre6/lib/i386;


      what to do?
      how to resolve it??

      Delete
    3. the url is properly written!! but still it is showing 404 error!
      and also the console is showing this..


      INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre6/bin/client;C:/Program Files (x86)/Java/jre6/bin


      wat am i supposed to do??
      how do i resolve this error??

      Delete
    4. Add "TOMCAT_HOME" System variable and check,,,Set the value to top level directory of your Tomcat install (e.g. E:\Projects\Java-web\apache-tomcat-7.0.33)

      Delete
  8. hi every one when i create a dynamique web project i'm getting this error failed while installing Axis2 core 1.1.
    NullPointerexeption.
    what does it mean?
    PLZ help me

    ReplyDelete
  9. hi every one when i create a dynamique web project i'm getting this error failed while installing Axis2 core 1.1.
    NullPointerexeption.
    what does it mean?
    PLZ help me

    ReplyDelete
  10. This is very nice tutorial. thanks a lot!

    ReplyDelete
  11. i am unable to create client saying the service definition selected is invalid plz help

    ReplyDelete
  12. CONGRATS MAN! Very helpful tutorial, tnx =D

    ReplyDelete
  13. CONGRATS MAN! Very helpful tutorial, tnx =D

    ReplyDelete
  14. hi my name is vishnu i have try u method but wsdl class file under the webcontent is not generated can u please help me

    ReplyDelete
  15. hi yhis vishnu i have try u method wsdl file is not generated under webcontent only axis2,meta inf,web-inf file are created

    ReplyDelete
    Replies
    1. Hello Vishnu,

      Right click on ur project -> Run as ->Run on server ->Select the server (Server should be running)-> Click on services -> Click on the the service link-> U will get the WSDL file open for u.

      Delete
  16. Good job! I tried lots of step by step tutorial, but for the first time my test project worked successfully, even if my eclipse version is not juno. I tried this tutorial in Eclipse Kepler.

    Thank you very much.

    ReplyDelete
  17. Good job! I tried lots of step by step tutorial, but for the first time my test project worked successfully, even if my eclipse version is not juno. I tried this tutorial in Eclipse Kepler.

    Thank you very much.

    ReplyDelete
  18. Tutorial nice...but while running on server it shows 404 error...

    ReplyDelete
  19. Tutorial is nice ...while running on server it shows 404 error

    ReplyDelete
  20. 4. Creating the web service

    Right click on your service class (not the project).
    Select Web Services >> Create Web Service

    In the above step I am not having that option "Webservices" in my service class...
    What could be the reason... Help me..........

    ReplyDelete
  21. hi.. when i create a web service i never get the "generate a default service.xml file" instead it shows a wsdl file window !!! please help !!!!

    ReplyDelete
  22. hi... whenever i create a web service by clicking on the class, i never get the "generate a default service.xml file" option, instead i get a wsdl file window.. what do i do ???! plzzz help !!!!

    ReplyDelete
  23. Thanks a lot..it is really helpful..keep it up..

    ReplyDelete
  24. Thank u very much... for ur post it is working fine

    But..
    My question is that our application is not Deployed in actual folder of Tomcat/webapps/axis2/webapps/OurAppn server

    How can i do this

    thank u in Advance

    ReplyDelete
  25. Thank you very much ...

    it help me alot

    ReplyDelete
  26. Someone knows how to solve this problem?

    Exception in thread "main" org.apache.axis2.AxisFault: Transport error: 404 Error: Not Found
    ...............................................................................
    ........................
    [INFO] Unable to sendViaPost to url[http://localhost:8080/........................

    ReplyDelete
  27. Someone knows how to solve this problem?

    Exception in thread "main" org.apache.axis2.AxisFault: Transport error: 404 Error: Not Found.......................................................................................

    [INFO] Unable to sendViaPost to url[http://localhost:8080/....................................................

    ReplyDelete
  28. Hi,
    How do you create diferent type of webservices, e.g. post and del method do you have any tutorials or any quick tips on how to do this? Nice tutorial by the way

    ReplyDelete
  29. hi.Waht is the part 2? Please provide the link

    ReplyDelete
  30. Hello, Please provide the link of 2nd part.

    ReplyDelete
  31. Hi,
    I have the main class where i have created the methods to be exposed via web-services, and the return object would be a different class.
    When i am trying to create the Web-services for this class, it is giving the error - Null pointer Error, that the additional class not found.
    How to include 2-3 classes while creating the web-services.

    ReplyDelete
  32. Hi,

    I am facing one problem, I have 2-3 classes and need to create the web-services of all the files, As I have some methods in one of the Services class, for which I am making the Web-Service by right click on that class, but at that time, it is not taking other helper classes while creating the web-services.
    And due to this, I am getting the Null-Pointer Exception.

    Please suggest how I can create the web-services using eclipse, when I have one service class and other helper class, as for the methods in service class, the return object is different class-object.- Something like - User Object class.

    ReplyDelete
  33. Very useful! Thanks alot for the effort you put into this!

    ReplyDelete
  34. SEO Service Provider Company is a Web Optimization provider company with a pioneer of providing WEB enabled services.

    ReplyDelete