Saturday 24 December 2011

Baby steps. Creating a simple web service in Eclipse/Weblogic/Java

We will be using Weblogic Application Server and Eclipse to develop a very simple Web Service. 
This tutorial serves two purposes:
  1. Clarifies some basic ideas about creating a web service.
  2. You can use it to create provider services for our OSB, ESB, BPEL examples.
So, lets get started. (Download source code here: Source Code (Eclipse Project)

This tutorial assumes that you already have Weblogic 11g installed in your system. You should also be having Eclipse installed in your system.

  • Start your Weblogic server. You need to click this:



  • Now start your Eclipse IDE. In my system, I have one shortcut in desktop.
  • Select File -> New -> Other


  • Select Web -> Dynamic Web Project


  • Give a suitable Project name and Choose to put it into an EAR project(optional).


  • Keep clicking next and finish. If all goes well, you will end up having a Web Project and an Enterprise Application in your workspace:


  • Right click on your dummy web project and click New-> Folder. Name it, say, resources. We will use it to keep our web service artifacts like xsd and wsdl.
  • Right click on your "resources" folder and select New->Other. Then in the dialog, select XML -> XML Schema. Click Next, give a suitable file name and hit Finish. You will have your XSD file opened in the default editor. Use this step twice to create two files, say, provider_req.xsd and provider_resp.xsd.
  • Here is how your request xsd will look like:




  • And here is how our response xsd will look like:



  • Again right click on your resources folder and do a New -> Other to open the dialog. Here choose Web Services -> WSDL and click Next. Give it a name, say, shipmentOrderSrvc.wsdl and click next. In the following screen, choose as shown in the following img. Remember to change the namespace.



  • You will have the wsdl file opened in your workspace. The file is big so the following screenshots split the whole wsdl in several parts:
  • Namespace declarations:



  • The types section of the wsdl:






  • The abstract part of the wsdl:





  • The concrete part of the wsdl:







  • The whole wsdl(sections shrinked):





  • To learn more about xsd and wsdl, please visit w3schools.com .
  • At this point you need to start your server, either from within Eclipse or separately from outside. If you wish to start your server from eclipse, you need to configure it first. These steps can help  http://simplifiedsoa.blogspot.com/2011/12/baby-steps-adding-weblogic-app-server.html :
  • Once you have started the server, you need to create the Java stubs and other classes for your service. No worries, you can do this with right and left clicks only. 
  • Right click on the wsdl you just created. Then select New -> Other and from the dialog, choose Web services -> Web service. Clicking next will start the wizard.






  • In the New Web Serivce wizard, everything is straight forward:


(Note:- For both of the app server and web service runtime, you can choose different values, here we choose Weblogic + Axis as we are using Weblogic Server and Apache Axis comes a web service runtime with it.


  • Now hit finish. It will take some time before you have a big number of packages and classes created in your workspace, something like this:




  • Its a good idea to browse through the set of classes and observe how they are formed. Notice the package names; they are almost opposite to what your namespace was, right?
  • In your workspace, you will also find other new artifacts like these:



  • Before your start coding for your web service, and there is not much left for that :), you should have a look at each of these artifacts. They are pretty much self explanatory and will help you better understand the working of the system.
  • Ok. Straight to coding. Open up the file named ShipmentOrderSrvcSOAPImpl




  • So now that we have our code ready, lets not wait anymore. Startup your server and run your application.
  • Remember, you need to be in the J2EE perspective to find the "Servers" tab the bottom of the page. You will find the configured Weblogic server here. Right click and click start.




  • After the server starts, deploy your application. Right click on your application -> Run as -> Run on server:





  • You will see the dialog that will let you choose the app server. Choose it and hit finish.
  • It will take some time before your application gets deployed.
  • After deployment, open the generated wsdl file, i.e., this one:




  • After you open this file, scroll down to the bottom of it and you will find the endpoint, i.e., where your service can be found on the network. Copy it:




  • Open SoapUI. (This is a software you need to use to test your services. Its free and you can get it at soapui.com.)
  • Right click on projects and click new Project.





  • In the dialog that pops up, Enter the endpoint followed by ?wsdl in the Initial WSDL field. Hit Ok.



  • In the left panel, you will find something like this:



  • Double click Request1. It will bring up the SOAP request:



  • You need to put some dummy values in the above step.
  • Now hit the green arrow on the top left. Soon after that you will see the response in the adjoining right window:



  • Ok. We are done here. If you find some problem, just put a comment here and I will be back with some solution, if I can. :)


4 comments:

  1. Uploaded Source code to accompany this tutorial.
    The link is in this line during the beginning of the post.

    So, lets get started. (Download source code here: Source Code (Eclipse Project)

    ReplyDelete
  2. can u plz tell the procedure for same in weblogic without using ant.
    Thanks

    ReplyDelete
  3. This is really helpful.. thanks for such a detailed illustrations.

    ReplyDelete
  4. The blog post is written so aptly.

    ReplyDelete