Create a simple Web Service from Java Code using JDeveloper

This posts covers the steps to create a simple department details web service that can be used with other examples.

We will use bottom up development, meaning we will create a new web service based on existing Java code.
The following instructions will create a new application and add a project to contain the web service.

Create a New Application and Project

Start JDeveloper.
Select Application-> New from the Main Menu.
In the Create Generic Application dialog set the application Name to WebServiceExamples
Select Generic Application and choose the Next button


New_App

In the next dialog set the Project name to DepartmentDetailsService
and select Java from the Project Template list, use the >> to add Java to the Selected column.

New_Project

Select the Next button and set the Default Package to  departmentdetailsservice

set package

Select Finish to create the new project.


Create the source files that implement the service

Next we will create 3 Java classes to implement the service.

DepartmentFinder.java     - This is the main class, it will return the details for a given department number.
DepartmentDetails.java    - This java bean contains the department details for a given department.
Fault.java                         - This class represents a ault that can be returned when an error occurs.

Add a new Java Class

In the Application Navigator pane, right click on the DepartmentDetailsService project and select New.
Select Java from the General Category on the left and select Java Class from the item list on the right.
Press the Ok button.

The Create Java Class dialog will appear.
Set the class name to DepartmentFinder and press the ok button to create the class.

New Java Class

Next open the DepartmentFinder.java class and replace the default file contents
with the code in the following file.

DepartmentFinder.java

Save the file.

Next repeat the 'Add a new Java Class' steps above
to create a new file named DepartmentDetails

DepartmentDetails.java

and again to create the the DepartmentFinderFault file

DepartmentFinderFault.java

and one last time to create the DepartmentFinderFault file

FaultBean.java

Compile the project and test as a simple Java Class

Right click on the DepartmentDetailsService and choose Make DepartmentDetailsService.jpr
The log pane at the bottom of Jdeveloper should show the following message:

Successful compilation: 0 errors, 0 warnings.

We can test the class using the main() method in the DepartmentFinder class.
Right click on the DepartmentFinder.java file in the Application Navigator and choose Run

A number of positive and negative tests are executed.

The Log window in JDeveloper should contain the following:

 Finding known department 2
 Department Number: 2,  Name: Inside Sales,  Cost Center: 22,  Org: Sales,  Mgr Email: peterBaines@westco.com
Test Passed

Get all departments
[3, 2, 1, 4]
Test Passed

Get department by Manager
 Department Number: 2,  Name: Inside Sales,  Cost Center: 22,  Org: Sales,  Mgr Email: peterBaines@westco.com
Test Passed

Finding unknown department 8
Test Passed

Get department using non-existent Manager
Test Passed

Finding with null department
Caught Exception: Department number cannot null
Test Passed

Process exited with exit code 0.


Convert the Java Class into a Web Service

To convert the Java class into a web service,
Right click on the DepartmentFinder.java file in the Application Navigator pane
and choose Create Web Service.
In the Create Web Service wizard, accept the defaults for each page by selecting the Next button.
Press the Finish button to create the web service.
If you examine the DepartmentFinder.java source you will see that a @WebService annotation has been added before the class definition.


Deploy the Web Service Project

Right click on the DepartmentDetailsService and choose Deploy->Webservices
In the Deploy WebServices wizard dialog select Deploy to Application Server and press the Next button.
Choose the correct name of a running WLSserver and accept the defaults to deploy the application.
After successful deployment the Deployment tab in the Log pane should show the following message.

[03:11:07 PM] Application Redeployed Successfully.
[03:11:07 PM] The following URL context root(s) were defined and can be used as a starting point to test your application:
[03:11:07 PM] http://10.0.2.15:7001/WebServiceExamples-DepartmentDetailsService-context-root
[03:11:07 PM] http://10.0.2.15:8001/WebServiceExamples-DepartmentDetailsService-context-root
[03:11:07 PM] Elapsed time for deployment:  4 seconds
[03:11:07 PM] ----  Deployment finished.  ----




Test the Web Service using Enterprise Manager Fusion Control

Connect to the Enterprise manager console using a browser, and supply the credentials defined for your weblogic server
For example  http://localhost:7001/em    weblogic/welcome1

Expand the Farm node and locate the Application Deployment named
WebServicesExamples-DepartmentDetailsService-context-root(AdminServer)
The server name in parenthesis indicates the server hosting the deployment, your server name may differ from the one shown above.
Select the node and press the Test Icon under the Web Services Section in the right pane.

Em_test

A test page will be generated for the service.

Supply a department number of 2 in the arg0 input field
Then press the Test Web Service button.

test_data


After a second or so the Response button tab should be automatically selected and the service results displayed.

test_result

This service is now available and can be consumed by Mediator or BPEL components.
Note the url for the WSDL of the deployed web service is available in the WSDL field in the top of the test page.
For example
http://10.0.2.15:7001/WebServiceExamples-DepartmentDetailsService-context-root/DepartmentFinderPort?wsdl

Pasting this url into a browser should return the wsdl for the service.
This url will be required when creating partner links to the service.


Finishing Touches

Additional annotations can be added to customize the wsdl generation.
For example lets set the method parameter names to be more descriptive than arg0, arg1

This can be accomplished with a few simple changes.
For example, by adding @WebMethod and @WebParam annotations to the method we can specify that the
input parameter name in the generated wsdl should be 'mgrEmailId rather than arg0

Here is the updated getDepartmentByMgrEmailId() showing both @WebMethod and @WebParam annotations

    @WebMethod
    public DepartmentDetails getDepartmentByMgrEmailId(@WebParam(name = "mgrEmailId")
        String mgrEmailId) throws DepartmentFinderFault {

The same changes can be applied to the getDepartmentDetails method.
  /*
  * Returns  the details for the selected department
  * @param   deptNumber   the department number
  * @return  a DepartmentDetails object containing the department details for the selected department or null
  */
    @WebMethod
    public DepartmentDetails getDepartmentDetails(@WebParam(name = "deptNumber")
        String deptNumber) throws DepartmentFinderFault {

Updating the WSDL

Changes to the service interface such as adding annotations require the wsdl to be regenerated.
Right click on the DepartmentFinder.java source file and choose->  Web Service Properties from the Context Menu.
In the resulting dialog select the methods entry, confirm the available get Methods are all selected and Press the OK button.
The WSDL will be regenerated

Download the completed example as a zip file from GitHub Here