Developing Contract-Driven Web Services Using JDeveloper

In this tutorial, you use JDeveloper to develop a Java web service using a top-down approach.

Approximately 30 minutes

Topics

This tutorial covers the following topics:

Creating the WSDL Contract Document
Generating the Web Service from the WSDL Contract Document
Testing the Web Service

Place the cursor over this icon to load and view all the screenshots for this tutorial. (Caution: This action loads all screenshots simultaneously, so, depending on your Internet connection, may result in a slow response time.)

Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.

Overview

Web services are discrete reusable software components that can be incorporated into applications. They build upon existing XML and HTTP protocols and use the following standards:

WSDL (Web Services Description Language): an XML-based language for defining web services
SOAP (Simple Object Access Protocol): an XML-based protocol for accessing services on the Web by using XML syntax to send commands over HTTP
UDDI (Universal Description, Discovery, and Integration): a universal catalog of web services that enables software to discover and integrate with services on the Web

JDeveloper provides many features to help you to create and deploy web services and to find existing web services to use in your applications.

The top-down web service development paradigm means that you start with the WSDL and generate all the classes and service artifacts using the definition of types and operations in the WSDL. This is more complex to achieve than bottom-up development, which allows existing systems to be wrapped as web services. But is bottom-up development the best approach? Many experts say that SOA is a business-driven architectural style, and for it to be successful it must employ a top-down approach.

In this tutorial you use JDeveloper's tools for end-to-end contract driven development, including XML schema creation, WSDL editing, top-down web service generation, and client creation and testing.

Back to Topic List

Prerequisites

Before starting this tutorial, you should:

1.

Have installed Oracle JDeveloper (10.1.3.1).

2.

Have started JDeveloper by double-clicking <jdev_home>\jdeveloper.exe.

If you receive a message asking if you want to migrate from a previous version, click No.


3.

Start Oracle Application Server Containers for J2EE (OC4J) by double-clicking <jdev_home>\jdev\bin\start_oc4j.bat.

If prompted for a password, enter welcome1. The password does not display as you enter it. You will have to reenter it on the next line to confirm. If you are not asked for a password, for later steps you will need to know what password was set when OC4J was initially started.

Watch the resulting command window and wait until you see the message 'Oracle Containers for J2EE 10g <10.1.3.1.0> initialized'. Then you can minimize the window, but do not close it.


4.

Create a connection in JDeveloper to OC4J:

1. Click the Connections tab (If the Connections tab is not visible, choose View > Connection Navigator from the menu).
2. Right-click the Application Server node and select New Application Server Connection from the context menu.
3. If the Welcome page of the Create Application Server Connection wizard displays, click Next.
4. On the Type page of the wizard, enter the Connection Name of oc4jconn and click Next.
5. On the Authentication page of the wizard, enter a Password of welcome1 (or whatever password was used when OC4J was initially started) and select Deploy Password, then click Next.
6. On the Connection page of the wizard, click Next.
7. On the Test page of the wizard, click Test Connection. After testing, the Status message should display Success!. (If it displays error messages, click Back and correct the connection information that you have entered.)
8. Click Next, then Finish.


Back to Topic List

Creating the WSDL Contract Document

To create the WSDL contract document, perform the following steps:

Creating a Schema

You first create a schema, and add some elements to it. You then import the schema into a WSDL document.

1.

In the Application Navigator, right-click the Applications node and select New Application from the context menu.

 

2.

In the Create Application dialog, enter TopDownContractDevelopment as the application name and select the No Template option. Click OK.


3.

In the Create Project dialog, enter the Project Name WSDLDocument and click OK.

 

4.

Right-click the WSDLDocument project and select New from the context menu.

 

5.

In the New Gallery, expand General in the Categories list and select XML; then from the Items list select XML Schema. Click OK.

 

6.

In the Create XML Schema dialog, enter a Name of CreditRating.xsd, then click OK.

The schema is created and opened in the editor.

 

7.

Select the schema in the editor, and click in the box to the right of targetNamespace. Type http://myCompany.com/CreditRating/types over the example name.

 

8.

You now begin to add elements to the schema. Elements are like variables that are global to the web service.
You can use the design editor to add elements.

In the design editor, click exampleElement, and type ssn as the name.

 

9.

Right-click ssn and select Set Type from the context menu.

 

10.

Click the arrow on the right of the box and choose xsd:string from the dropdown list.

 

11.

Add another element. You can do this by dragging the element component from the Component Palette and dropping it onto <schema>.

 

12.

Click inside the element and overtype the default name with rating.

.

 

13.

Set its type to xsd:int, by using the dropdown list as before.

 

 

14.

In the same way, add a third element with the name error and a type of xsd:string.

Click Save All Save All button to save your schema.

 

 

Creating the WSDL Contract Document

Next you create the WSDL contract document, and import the schema you created in the previous steps, into it.

1.

Right-click the WSDLDocument project and choose New from the context menu. Expand the Business Tier node in the New Gallery, and choose Web Services in the Categories pane, and WSDL Document in the Items pane.

 

 

2.

Type CreditRating as the WSDL Name, and http://myCompany.com/CreditRating as the Target Namespace. Click OK.

 

3.

Notice that the CreditRating WSDL has been created and opened in the editor. Ensure that you are in the Design tab for the editor window and the Structure window. If not, click the Design tab at the bottom left in each window. The windows should look like the screenshot below.

 

 

4.

In the Structure window right-click Types and choose Insert types... from the context menu.

 

 

5.

Right-click the types sub-node and from the context menu choose Insert inside types-->XML Schema.

 

 

6.

In the Insert XML Schema Item dialog, choose schema and click OK.

7.

Right-click the schema sub node in the Structure window, and choose Insert inside schema-->import.

 

8.

Ensure that the Properties Inspector window is visible. If it is not, choose View-->Property Inspector.
In the PI, specify the location of the schema (click ... to browse for it if you wish). Specify CreditRating.xsd as the id and http://myCompany.com/CreditRating/types as the schema namespace.

 

9.

Click the Source tab in the editor to view the code for the CreditRating.wsdl file. Right-click and choose Reformat to tidy up the code and make it more readable.

 

10.

Now that you can see the values in the Definitions tag more clearly, add the following line at the end of the tag:

xmlns:types="http://myCompany.com/CreditRating/types"

Click Save All Save All button to save the WSDL document containing the imported schema.

Back to Topic

Defining the Service Interface

Now you add the abstract or reusable elements of the WSDL. This is all the developer might create at this stage to allow others to use the WSDL to generate the underlying Java implementation class that they must code to.

You define the service interface by specifying:

In this example, you define one operation for this service that gets the credit rating of the loan applicant. To define the service interface, perform the following steps:

1.

First you add three messages.

In the editor window, click the Design tab of CreditRating.wsdl, then click Switch View so that the Messages element is in the left-most position. This step is not necessary, but makes it easier to see what you are creating first.

 

2.

In the Structure window, select the Design tab.

Right-click Messages and select Insert Message from the context menu.


3.

In the New Message dialog, enter the Message Name CreditRatingRequestMessage, then click OK.

 

4.

In the Structure window, right-click message - CreditRatingRequestMessage and select Insert Inside Message - CreditRatingRequestMessage > http://schemas.xmlsoap.org/wsdl/ > part.

 

5.

In the New Part dialog, enter a Part Name of ssn and types:ssn as the Part Type.

 

6.

In the same manner as in steps 2 - 5, add the following messages and their corresponding part types:

Message Name Part Name Part Type
CreditRatingResponseMessage rating types:rating
CreditRatingFaultMessage error types:error

After adding the messages, you can click on any element in the Structure window or the Design tab of the editor and you are able to see and modify its values in the Property Inspector.

 

7.

Now you define the PortType, which is the service interface, the equivalent of the method signature that specifies operations and the messages (parameters) that they use.

In the Structure window, right-click PortTypes and select Insert portType.

 

8.

In the New Port Type dialog, enter the Port Type Name of CreditRating, then click OK.

 

9.

In the Structure window, right-click the CreditRating portType and select Insert inside portType -Credit Rating > operation.

 

10.

In the New Operation dialog, enter the Operation Name processCreditRating.

The Operation Type should be Request Response.

From the Input drop-down list, select tns:CreditRatingRequestMessage, which is one of the messages you defined earlier.

From the Output drop-down list, select tns:CreditRatingResponseMessage, which is another of the messages you defined earlier.

Select Add Fault.

Enter a Fault Name of NegativeCredit.

From the Fault drop-down list, select tns:CreditRatingFaultMessage, which is the third message you defined earlier.

Click OK.

 

11.

Expand the CreditRating Port Type in the WSDL editor to see how the messages are associated with the operation. Click the different components of the operation to see the messages associated with each.

Note that the request message is associated with the input to the process, the response message is associated with the output from the process, and the fault message is associated with the NegativeCredit named fault.

 

12.

The final part of creating the service interface is defining the bindings. You bind the Port Type to a specific protocol (HTTP) and data format (SOAP).

In the Structure window, right-click Bindings and select Insert binding from the context menu.

 

13.

In the New Binding dialog, enter a binding Name of CreditRatingSoapHttp.

Leave all other fields as defaults. The Port Type defaults to the Port Type that you created.

Click OK.

 

14.

In the design editor, expand the binding that you just created to see the details.

Notice that when you click any element, the Design tab of the editor shows its relationship to other elements.

 

15.

In the editor, click the Source tab.

Scroll to the NegativeCredit fault definition toward the end of the file. Change the line:

<soap:body use="literal" parts="ssn"/>

to:

<soap:fault use="literal" name="NegativeCredit"/>

 

Back to Topic

Defining the Service Implementation

The final step in creating a WSDL document is to create the service implementation definition. This definition includes the port that specifies the binding that a particular implementer is going to implement and possibly the server where the service will run.

To define the service implementation, perform the following steps:

1.

In the Design tab of the Structure window, right-click Services and select Insert service from the context menu.

 

2.

In the Insert Service dialog, enter a name of CreditRatingService, then click OK.


3.

In the Structure window, right-click service - CreditRatingService, then from the context menu select Insert inside service - CreditRatingService > http://schemas.xmlsoap.org/wsdl/ > port.

 

4.

In the New Port dialog, enter a Port Name of CreditRatingServiceSoapHttpPort. The Binding Name defaults to the binding that you created. Click OK.

 

5.

In the Structure window, right-click port - CreditRatingServiceSoapHttpPort, then from the context menu select Insert inside port > soap:address.

 

6.

In the Insert Address dialog, for the location enter any string, such as tbd, because it is not necessary at this stage to specify where the finished service will run. Click OK.

 

7.

In the Application Navigator, right-click CreditRating.wsdl and select Validate WSDL from the context menu.

The Message tab in the log window tells you if there are any errors, and the Source tab in the Structure window gives you details of any errors.

 

8. Click Save All Save All button to save your work.

 


Back to Topic List

Generating the Web Service from the WSDL Contract Document

To generate the web service, perform the following steps:

1.

In the Application Navigator, right-click the TopDownContractDevelopment application and select New Project from the context menu.

In the New Gallery, select Empty Project in the Items list and click OK.

 

2.

In the Create Project dialog, enter a Project Name of Service, then click OK.


3.

Right-click the Service project and select New from the context menu.

In the New Gallery, expand Business Tier and select Web Services in the Categories list, then select Java Web Service from WSDL in the Items list. Click OK.

 

4.

Click Next on the Welcome Page of the Create J2EE 1.4 Java Web Service from WSDL wizard.

On the Web Service Description page of the wizard, from the WSDL Document URL drop-down list select the CreditRating.wsdl that you just created. Click Next.

 

5.

On the Default Mapping Options page of the wizard, you could click Next to page through all the options. However, because you are accepting all the defaults, you can just click Finish.

Now you have a full web service and all the JAX-RPC files to deploy and run the service.

 

6.

In the Application Navigator, expand Application Sources and the service package under the Service project.

Note that JDeveloper has created a new service called CreditRatingService. Select CreditRatingService in the Application Navigator to see other associated files displayed in the Structure window.

 

7.

The CreditRatingService.wsdl file opens in the editor. Click the Design tab. Under Services, expand CreditRatingService and CreditRatingServiceSoapHttpPort.

Select soap:address and look at its Property Inspector. You can see that the location, which you had specified as tbd, has now defaulted to your local machine. This changes depending on where you actually deploy the service. This soap address, or endpoint, is the location (URI) of the web service on the network.

By default, JDeveloper creates WSDLs with an endpoint that uses port 8888, because that is the default that would be used with an external server. Because you are testing the web service with the embedded (not external) OC4J, you should change the default port number to the port being used by the embedded OC4J server. To see what port that is, you can invoke Tools > Embedded OC4J Server Preferences, then click Startup in the tree at the left. The port number for the embedded server is shown in the HTTP field. Once you have obtained the port number, you can dismiss the preferences window.

If the soap:address in the WSDL is different from that of the embedded OC4J port, change it to the correct port number.

 

8.

Select CreditRatingService in the Application Navigator and in the Structure window double-click CreditRatingImpl.java to open it in the editor. Notice that it takes a String as a parameter and that the name of the parameter is ssn, which is the name of the Message parameter that you created earlier.

You need to add the actual implementation of the method. Replace:

return 0;

with:

int id;
try
{
// Parses integer value of first 3 numbers form SSN
id = Integer.parseInt(ssn.substring(0,3));
}
catch(NumberFormatException e)
{
// if SSN is invalid returns -1
return -1;
}
if(id < 300)
{
// If value of the first 3 numbers from customer SSN is less
// than 300, credit rate is 1.
return 1;
}
else if(id < 600)
{
// If value less than 600, credit rate is 2.
return 2;
}
else if(id <900)
{
// If value less than 900, credit rate is 3.
return 3;
}
else
{
// Otherwise, credit rate is 0.
return 0;
}

 

9.

You also need to ensure that CreditRatingImpl.java correctly implements the endpoint interface CreditRating.java.

In the interface, the processCreditRating() method throws a CreditRatingFaultMessage exception, so check to see that the processCreditRating() method in CreditRatingImpl.java also throws a CreditRatingFaultMessage exception. If it does not, add the following code to the processCreditRating() method signature in CreditRatingImpl.java:

throws CreditRatingFaultMessage

Click Save All Save All button to save your work.

 

10.

In the Application Navigator, right-click CreditRatingService and select Run from the context menu. This deploys the web service to JDeveloper's embedded OC4J server. You should see a message in the log window indicating that the web service has been started.

Back to Topic List

Testing the Web Service

Now you test the web service by creating a client to call it. To test the web service, perform the following steps:

1.

In the Application Navigator, right-click the TopDownContractDevelopment application and select New Project from the context menu.

In the New Gallery, select Empty Project in the Items list and click OK.

 

2.

In the Create Project dialog, enter a Project Name of Client, then click OK.


3.

Right-click the Client project and select New from the context menu.

In the New Gallery, expand Business Tier and select Web Services in the Categories list, then select Web Service Proxy in the Items list. This uses the web service's WSDL to generate a client proxy that has all the public available data interfaces as the web service. Client applications can then use the proxy object to access the web service.

Click OK.

 

4.

If the Welcome page of the Create Web Service Proxy wizard displays, click Next.

On the Web Service Description page of the wizard, select CreditRatingService.wsdl from the drop down list. Selecting this file populates the Mapping File field. Click Next.

 

5.

On the Port Endpoints page of the wizard, select the default radio button - Run against a service deployed to Embedded OC4J.

You can click Next to tab through the pages if you wish. However, you are accepting all defaults, so you can just click Finish.

The file CreditRatingServiceSoapHttpPortClient.java is created and automatically opened in the editor window.

 

6.

You need to add code to call the web service. In the code editor, after the line:

// Add your own code here

Add the following code:

int rating = myPort.processCreditRating("78964022");
System.out.println("Credit Rating is " + rating);

 

7.

Click Run Run button. The web service returns its results to the log window.

Back to Topic List

In this tutorial you used JDeveloper's WSDL editor to develop a web service with the top-down approach. You tested the web service by creating and running a web service client proxy.

Back to Topic List

To learn more about using web services, refer to:

Place the cursor over this icon to hide all screenshots.