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


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.


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.     - This is the main class, it will return the details for a given department number.    - This java bean contains the department details for a given department.                         - 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 class and replace the default file contents
with the code in the following file.

Save the file.

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

and again to create the the DepartmentFinderFault file

and one last time to create the DepartmentFinderFault file

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 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:
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:
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 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 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]
[03:11:07 PM]
[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
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.


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.


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


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

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

    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
    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 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