Developing Contract-Driven Web Services Using Jdeveloper

Total Page:16

File Type:pdf, Size:1020Kb

Developing Contract-Driven Web Services Using Jdeveloper

Abhishek Rakshit

Tutorial Assignment

Developing Contract-Driven Web Services Using JDeveloper

To create the WSDL contract document, we have to perform the following steps: Create a schema Create a WSDL contract document Define the service interface Define the service implementation

Prerequisites

Before starting: 1. Have installed Oracle JDeveloper (10.1.3.1). 2. Have started JDeveloper by double-clicking \jdeveloper.exe. 3. Start Oracle Application Server Containers for J2EE (OC4J) by double-clicking \jdev\bin\start_oc4j.bat. 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. On the Type page of the wizard, enter the Connection Name of oc4jconn and click Next. 4. 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.

5. On the Connection page of the wizard, click Next. 6. Click Test Connection. After testing, the Status message should display Success!. 7. Click Next, then Finish.

Create a Schema 1. In the Application Navigator, right-click the Applications node and select New Application. 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. 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. 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 . 12. Click inside the element and overtype the default name with rating. 13. Set its type to xsd:int. 14. In the same way, add a third element with the name error and a type of xsd:string.

15. Click Save All to save your schema. Creating the WSDL Contract Document

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. 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. In the PI, 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. 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"

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

Defining the Service Interface

1. 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. 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: 7. 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 Structure window, right-click the CreditRating portType and select Insert inside portType -Credit Rating > operation.

11. Enter the following: 12. 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.

13. In the Structure window, right-click Bindings and select Insert binding from the context menu. 14. In the New Binding dialog, enter a binding Name of CreditRatingSoapHttp. 15. In the editor, click the Source tab. soap:body use="literal" parts="ssn"/> to:

Defining the Service Implementation

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. 8. Click Save All to save your work.

Generating the Web Service from the WSDL Contract Document

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. 6. In the Application Navigator, expand Application Sources and the service package under the Service project. 7. 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. 8. 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. 9. You also need to ensure that CreditRatingImpl.java correctly implements the endpoint interface CreditRating.java. throws CreditRatingFaultMessage

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: 10. Click Save All to save your work. 11. 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. Testing the Web Service

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. 4. 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. 6. Add the following code: int rating = myPort.processCreditRating("78964022"); System.out.println("Credit Rating is " + rating); 7. Click Run. The web service returns its results to the log window.

Recommended publications