Showing posts with label RAD. Show all posts
Showing posts with label RAD. Show all posts

Steps for creating webservice client using RAD

In the previous article we have seen how to create server side component of webservice using RAD. Now we need to create a web service client and call the service from the client. Below are the steps to follow to create the client to access the web service:

  • Create a Project for the Client in the same workspace. Give the project name as DemoWebServiceClient (or any name of your choice).
  • Create the client stubs from the wsdl file of the web service provider.
  • Write a sample program in client to access the web service through the client stubs which were created.

Create the Client Project

To create a new client project perform the below navigation:

Right click on the Dynamic Web Project ->click on New -> click on Dynamic Web Project. Give the Project name as DemoWebServiceClient. All the values give as default as it is.

Next click on the Finish button. Now a new project added to your current workspace with name DemoWebServiceClient. The client EAR will be DemoWebServiceClientEAR.

Generate the Client Stubs from the WSDL file

Following are steps for how to create the client stubs from the wsdl file of the web service provider.

Go to the DemoWebService project wsdl file. The wsdl file will present in the WebContent\WEB-INF\wsdl\CalculatorService.wsdl folder.

Right click on wsdl file ->click on Web Services -> click on Generate Client.




RAD Interface

The forthcoming screenshots will provide the steps to how to create the client stubs from the web service wsdl file.




This screen will ask for client proxy type. Choose the default proxy type which is Java proxy. Now click on Next.





In the above screen enter the path of the wsdl file. The client stubs will be generated based on the wsdl file. Here in wsdl URI will be pre filled with the wsdl file location path. If you want to select an external wsdl file then browse and select the wsdl file. Now click on Next.



This screen will ask for the Client type, Client Project Name & Client EAR project where all the client stubs will be generated. Now in this case our client type is default which is Web, Client project is DemoWebServiceClient and the Client EAR is DemoWebServiceClientEAR. If you want to change the client server version then through the EDIT button you can change. Now click on Next.







This screen is for security setting in web service client. But here noting to change and click on Finish.



Project Explorer

Below client stubs files will be created in the Client EAR for accessing the remote web service. The Client stubs are:

1. CalculatorService.java
2. CalculatorServiceProxy. java
3. CalculatorServiceService.java
4. CalculatorServiceServiceInformation.java
5. CalculatorServiceServiceLocator.java
6. CalculatorServiceSoapBindingStub.java

The CalculatorService.java is the remote interface which contains all the abstract methods of the web service.

The CalculatorServiceServiceLocator.java will contain the address of the remote server where the web service is deployed.

Access the web service from the client

Write a sample web service client program to access the service. Below is the client program which is accessing the remote web service.

Client Program Description

To get the address of the remote web service deployed server you need to create the object of CalculatorServiceServiceLocator class. Through the getCalculatorService() method of CalculatorServiceServiceLocator will get the reference of the CalculatorService (Remote Interface). Now invoke the service methods on CalculatorService which internally invoke the actual service method and give the results. Here the CalculatorServiceSoapBindingStub class contains the XML parsing implementation between the java and xml data type conversion. The client will communicate through SOAP over HTTP with the remote web service deployed server. The client stubs contains the JAX-RPC implementation for platform independent communication.



Execute the Client Application

Find the below navigation to execute the client application in RAD.

Navigation: Right click on DemoServiceClient.java -> click on Run -> Click on Java Application.



Check the results in the Console window of RAD.

Steps for creating the Server Side Component of WebService using RAD

There are few steps that needs to be followed in order to create server side component of web service even if you are using IDE like RAD but they are very simple ones. Let create a simple calculator service exposed as web service.

Step1 : Create the Web Service Implementation class

Create a new Dynamic Web Project named as DemoWebService. Follow the below navigation: File>New >Dynamic Web Project. So the web project will automatically create in the workspace which we were created initially. Here the default target server is WebSphere v6.0.


Step1
Step1


Create a package in Java Resources as demowebservice and create CalculatorService class in that package. This class is the implementation class of the web service provider. All the business logic of the web service will be put in this class.

Step2
Step2



Below are the methods present in CalculatorService class. This is a sample example to test the web service so this particular class contains four arithmetic methods. Each method taking two parameters and returns the corresponding result depends upon the operation.
CalculatorService Web Service SOAP Implementation Class:


Calculator Service
Calculator Service


Now publish the CalculatorService.java file as a web service. Below is the navigation to how to make the class as a web service server component.

1.1. How to make the java class as a web service component

Right click on the class CalculatorService->Go to Web Services -> Click on Create Web Service.



RAD
RAD



After clicking on the Create Web Service following screens will come. The creation of web service in RAD internally uses the web sphere commands such as WSDL2Java, JAVA2wsdl etc to create the web service related xml file and other web service supporting files.


Screen1
Screen1


Screen 1:
This step will ask about the web service type. The Web Service Type contains Java bean Web Service, EJB Web Service, URL Web Service etc .The default Web Service Type in RAD is Java bean Web Service. Don’t change any values in screen 1 and click on Next button to go to the next step to select the web service implementation class.

Screen2
Screen2


Screen 2:

This screen will only ask for the implementation class of the web service. Here our implementation class is present in the package demowebservice. This Bean class contains all the business logic related to web service. The web service implementation class (demowebservice.CalculatorService) will automatically selected. If you want any external implementation class click on Browse Classes to select the file and click on Next button.

Screen3
Screen3


Screen 3:

This screen will confirm the server and J2EE version on which the web service will be deployed. You can change the server & J2EE version by clicking on the Edit button.

The Service Project will contain the current dynamic web project name where the web service is going to create.

The EAR project will contain the EAR name of the current project which contains the entire source code of the web service component. Automatically the Service Project & EAR Project will be selected depending upon the class which you want to make it as a web service. Click on Next to select the SEI (Service Endpoint Interface) for the web service.

Screen4
Screen4


Screen 4:

The above screen 4 will give the service end point interface of the bean class. So in the Service End Point interface text box the appropriate SEI will be displayed as disabled. Then click on Next.

Screen5
Screen4


Screen 5:
This screen will ask for all the configurations before creating the web service. The Web Service URI contains the actual location of the web service where it deployed. The wsdl folder contains the location of the wsdl file. All the methods which are present in the bean implementation class will be displayed in the Methods section with check boxes. The methods can modify by deselecting the checkboxes in Methods section. The default style of the web service in RAD is Document/Literal. Most of the language prefers this type of style in web service. The Security configuration combo box contains the different types of security implementations in web services. The security implementations are XML Digital Signature, XML Encryption etc. Here we are not implementing any security, so don’t change any thing and click on next.

Screen6
Screen6


Screen 6:
This screen will ask for publishing the web service in UDDI. If you want your web service as public then publish through UDDI is necessary. Don’t modify any settings and click on Finish.

Project Explorer
Project Explorer


This Screen Shot is the Project Explorer of DemoWebService Project. After creation of web service the below new files were created in the project.

demowebservice\CalculatorService_SEI.java: This Service End Point interface contains all the methods of bean implementation class (CalculatorService.java) in an abstract way.

WebContent\WEB-INF\wsdl\CalculatorService.wsdl: This is the web service description file which contains web service method input & output parameters return type, document style, service provider end point description etc.
Below are the files which will be created related to web service.

  • CalculatorService_mapping.xml
  • ibm-webservices-bnd.xmi
  • ibm-webservices-ext.xmi
  • webservices.xml
All the files will be created in the WebContent\WEB-INF directory. Below figure is a part of the CalculatorService.wsdl file:


WSDL
WSDL


Each method of the SEI will be put inside an element tag. The input parameters of the method will be put inside a complex type tag (<complexType></ complexType). The type attribute of inner element tag contains the xml equivalent data type of java data type. Similarly a new element tag will be created for each method based upon the method return type. The return type of method will be maintained as a separate element tag with name addition with Response. For ex: for addition method the element name will be additionResponse.

WSDL
WSDL


The above code snippet is the service portion of the wsdl file which contains the server address, Port binding information & service name of the web service.

The service component name will be the <SEI name>+ Service. So here the service end point interface name is CalculatorService + Service. The Service name will be inside the <wsdl:service> tag. The web service deployed address will be in <wsdl:address> tag and the location attribute contains the actual address of the server.

Steps for creating a WebService using RAD IDE

By using Rapid Application Developer (RAD) you can create, deploy and test your web services. RAD internally contains WebSphere application server for deployment of application.

There are various ways to create a web service. You can create a web service from normal java class and also you can make a session Bean (EJB Type) as a web service.

In this article i will show you how a normal java class will be published as a WebService using RAD.

Step by Step creating a WebService using RAD

  • Create a New Workspace which will contain the server Dynamic Web Project.
  • Create the web service implementation server class inside a package. This class will be called every time when the client will invoke/call the web service.
  • Publish the web service implementation class as a web service so that it will create the necessary web service files (wsdl, SEI etc) in the project and it will act as a web service provider for the clients who want to access the service.
  • Finally create the client stubs from the web service wsdl file (which was generated before) to access the service from the client.
Open RAD from Start>Programs>IBM Rational>IBM Rational Application Developer 7.x>Rational Application Developer.

During start-up of the RAD it will ask for the workspace. So during start-up give the workspace as D:\Testing Workspace.You can create workspace with any name of your choice.


RAD IDE
RAD


Check the web services Development check box from RAD preferences. Follow the below navigation.

Navigation: Windows>Preferences. Expand the Work Bench from the left side.

Click on Capabilities & in the right side Check the Web Services Development Check Box.

This option is necessary to make a java class as a web service.


RAD IDE
RAD Preferences

Now the development environment is ready and we have to create two things.


Frequently used shortcut keys of RAD


Shortcut keys help by providing an easier and usually quicker method of navigating and using the features of the editor. It helps you to work smarter with the editor. Below are the shortcut keys for frequent activities in RAD. If the shortcut keys would be used more effectively, then open the window in java perspective.

Shortcut Keys:

#
Shortcuts
Description
1
Ctrl+shift+r   
Helps to choose a resource (Type some initial letters of the files, the files will be listed out)

2
Ctrl+o
use this shortcut in any of your java file, it will list out all the methods in that class

3
Ctrl+F4
Close the active file in your RAD editor.
4
Ctrl+Shift+F4
Close all the windows which have opened in the editor.
5
Ctrl+f
Helps to find/Replace the phrase or replace the phrase in a file.
6
Ctrl+k
Helps to find the next searched phrase
7
Ctrl+Shift+k
Helps to find the previous searched phrase
8
Ctrl+l
Helps to go the particular line number.
9
Ctrl+h
Helps to find the file, phrase, etc in the whole workspace.
10
Ctrl+F8
Switch to available editors
11
Ctrl+M
Maximize active view or editor
12
Ctrl+shift+s
Save All
13
Ctrl+Shift+f
Format Code
14
Ctrl+Shift+o
Organize Imports
15
F3
Show source for a method or a class
16
Ctrl+Shift+p
Show matching brace.
17
F6
In debug mode, For “step into” use this shortcut key to debug by executing each and every line.
18
F8
In debug mode, For “step over” use this shortcut key to debug for every breakpoint.
19
Ctrl+space
Content assist
20
Ctrl+b
Build All
21
Ctrl + /
Single line comment
22
Ctrl + Shift + /
Multiple line comments
23
Ctrl + T
Open Implementation.