Vous êtes sur la page 1sur 54

Developing, Deploying and Managing Web

Services Using
JDeveloper and Oracle Application Server

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 fo

SOAP (Simple Object Access Protocol): an XML-based protocol for access

UDDI (Universal Description, Discovery, and Integration): a universal cat


JDeveloper provides many features to help you to create and
deploy web services and to find existing web services to use in your
applications.
This tutorial shows you how to use some of JDeveloper's web services tools
to
create and optionally to monitor web services, and also how to use
Application
Server Control to manage a web service.

Scenario
In JDeveloper you would like to develop components for common

functions and deploy them as web services. You would also like to be able to
manage the web service for such aspects as logging and security, and monitor
the HTTP requests and responses generated by invoking the web service.

Prerequisites
Before starting this tutorial, you should:

1. Have installed the JDeveloper 10g (10.1.3).


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. Nothing displays as you enter the password. 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.0.0) initialized'. Then you can minimize the window, but do not close it.
4. Create a connection in JDeveloper to OC4J:

Click the Connections tab (If the Connections tab is not visible, choose View
> Connection Navigator from the menu).

Right-click the Application Server node and select New Application


Server Connection from the context menu.

If the Welcome page of the Create Application Server Connection wizard


displays, click Next.

On the Type page of the wizard, enter the Connection Name of oc4jconn and
click Next.

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.

On the Connection page of the wizard, click Next.

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

Click Next, then Finish.

Creating the Java Class

JDeveloper enables you to use an existing web service


or to create one with Java or PL/SQL. In this tutorial, you create a web
service
that is based on a Java class.

To create the Java class for the web service, perform


the following steps:

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 the Application Name of JavaWebService.


From the Application Template drop down list, select No Template [All Technologies].
Click OK.

3.

In the Create Project dialog, enter a Project Name of GetDates and click OK.

4.

In the Application Navigator, right-click the GetDates project and select New from the
context menu.

5.

In the New Gallery, select the General node in the Categories list, and then in the Items list
select Java Class. Click OK.

6.

In the Create Java Class dialog, enter:


Name: GetDates
Package: datespackage
Click OK.

The new Java class is created and displayed in the editor.

7.

In the editor, create a method getDate() that uses java.util.Calendar to return the date and
time.
public Calendar getDate()
{
return Calendar.getInstance();
}

8.

Because the code refers to a class that is not yet available to this package, you are prompted
to import the class. Press [Alt]+[Enter] to import the suggested class java.util.Calendar.

9.

In the editor, create a second method getDateHence() that uses java.util.GregorianCalendar


to add a user-defined number of days, specified as the int parameter daysHence, to today's
date. Add the following code below the getDate() method, pressing [Alt]+[Enter] when
prompted to import java.util.GregorianCalendar:
public Calendar getDateHence( int daysHence)
{
GregorianCalendar myCalendar = new GregorianCalendar();
myCalendar.add(GregorianCalendar.DATE, daysHence);
return myCalendar;
}

10.

Click Make

to compile the class. The log should show successful compilation.

Creating a Web Service on the Java


Class

To create a web service based on the Java class that you just
created, perform the following steps:

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

2. In the New Gallery, expand Business Tier in the Categories list and select Web Services, then

select Java Web Service from the Items list. Click OK.

3. In the Select J2EE Web Service Version dialog, select J2EE 1.4 (JAX-RPC) Web Service, then

click OK.

4. If the Welcome page of the Create Java J2EE 1.4 Web Service dialog displays, click Next.

In the Class page of the wizard

Enter GetDatesWS for the Web Service Name.

Select datespackage.GetDates from the Component To Publish dropdown list.

Select the "Generate annotations into class" check box.

Click Next.

5. On the Message Format page of the wizard, click Next.

On the Specify Custom DataType Serializers page of the wizard, click Next.
On the Methods page of the wizard, select the check boxes next to both methods from the

Available Methods list and click Finish.

Note: If you get an error about not being able to load the interface datespackage.GetDatesWS,
then repeat steps 1-5.

6. If it is not already open in the editor, open the WSDL document for the web service: Under the

GetDates project in the Application Navigator, expand Web Content and WEB-INF\wsdl, then
double-click GetDatesWS.wsdl.
Another way to open the WSDL is to right-click the GetDatesWS web service in the Application
Navigator and select Go to WSDL from the context menu.

7. Click the Source tab of the WSDL editor. Scroll to the bottom of the WSDL file and copy the

URL defined in the soap:address node. It should be similar to:


http://141.144.80.136:8888/JavaWebService-GetDates-context-root/GetDatesWSSoapHttpPort

8. If the file is still open, click the GetDates.java tab at the top of the editor, or reopen the

GetDates.java file by double-clicking it in the Application Navigator: (JavaWebService >


GetDates > Application Sources > datespackage > GetDates.java).
Note the Java 5 annotations (lines beginning with "@") that have been generated because of the
selection you made above. Inclusion of these annotations is optional and may not be identical to

those shown below.

9. Click Save All

to save your work.

Back to Topic List

Exploring
the WSDL Editor
There is another way to develop web services that is called
contract-driven, or top-down, development. This approach is more responsive
to business requirements, rather than simply creating a service layer in a
bottom-up
manner by wrapping existing systems using web services. In JDeveloper, the
top-down

approach uses the WSDL editor, rather than the wizard, to create a new WSDL
and generate all the classes and service artifacts.

You will not use contract-driven development in this tutorial.


However, now you can examine the WSDL editor to see what the wizard created
for you and how all the elements are related. To explore the WSDL editor,
perform
the following steps:

1. If the file is still open, click the GetDatesWS.wsdl tab at the top of the editor, or reopen the

GetDatesWS.wsdl file by double-clicking it in the Application Navigator: (JavaWebService >


GetDates > Web Content > WEB-INF\wsdl > GetDatesWS.wsdl)
Click the Design tab at the bottom of the editor to view the visual editor.
In the Services column, select the GetDatesWSSoapHttpPort node.

Observe how the content of the Bindings and Port Types columns are now highlighted and
connected.
Note: You may need to select View | Refresh from the JDeveloper menu in order to see all the

connections.

2. In the Bindings column, select the getDate node.

Observe how the getDate binding is linked to the getDate operation in the Port Types column.
Note: You may need to select View | Refresh from the JDeveloper menu in order to see all the
connections.

3. Expand the getDateHence binding node, and select the input node.

Observe how the GetDatesWS_getDateHence message is connected to getDateHence operation

which is in turn connected to the input node of the getDateHence binding.


Note: You may need to select View | Refresh from the JDeveloper menu in order to see all the
connections.

Back to Topic List

Deploying
and Testing the Web Service
To deploy and test the web service that you have created,
perform the following steps:

1. In the Application Navigator, expand the Resources node. Right-click WebServices.deploy, and

select Deploy to > oc4jconn from the context menu.


(Note: This assumes that you have started OC4J and have created a connection to it as described
in Prerequisites.)

In the Configure Application dialog, click OK.


The log window should show successful deployment.

2. Open a browser and paste the URL you copied from the WSDL file above, which is the

GetDatesWS endpoint page generated by OC4J.


The endpoint page for the GetDatesWS displays. Note the links to the two methods: getDate and
getDateHence.
Click getDate.

3. The getDate Test page displays an area where you can specify parameters for the method.

Because this method does not take any parameters, just click Preview SOAP.

4. The Preview of Request window shows you the SOAP request that will be sent. Click Invoke to

send the request and invoke the getDate method of the web service.

5. After you invoke the web service, the bottom part of the window displays the SOAP envelope

containing the result, which is today's date and time. It looks something like this:

<ns0:result>2005-11-15T16:51:26.566-07:00</ns0:result>

6. Click Back twice to return to the GetDatesWS endpoint page, then click getDateHence.

7. The GetDateHence method requires a parameter.

In the box provided for the daysHence parameter enter the number 5, to return the date five days
hence. Click Preview SOAP.

8. The preview shows the value 5 that will be passed as the daysHence parameter. Click Invoke.

After you invoke the web service, the bottom part of the window displays the SOAP envelope
containing the result, which is the date and time five days from today. It looks something like
this:
<ns0:result>2005-11-20T17:01:21.682-07:00</ns0:result>

9. Retest the GetDateHence method with a different value:

Click Back to return to the getDatesHence page.

In the parameter value field enter the number 365, to return the date a year
hence.

Click Preview SOAP .

Click Invoke.

The resulting SOAP envelope contains the result, which is the date and time
one year from today. It looks something like this:
<ns0:result>2006-11-15T17:04:57.282-07:00</ns0:result>

Leave the browser open for the next section.

Managing
the Web Service
To use Oracle Application Server Control to manage the web
service, perform the following steps:

1. Using the same server and port number, in the browser enter the URL for application server

control:
http://<server>:<port>/em
The Application Server Control login screen displays.
Enter a username of oc4jadmin and a password of welcome1 (or whatever password was used
when initially starting OC4J), then click Login.

2. On the home page, click the Web Services tab.

3. Click the GetDatesWSSoapHttpPort link.

4. You can view performance information from the service port home page. You can see the

number of times the web service has been invoked, the number of faults, and the average
response time in milliseconds.
Click the Operations tab.

5. On the Operations tab you can view the performance information for the getDate and

getDateHence operations:
1. Total Requests
2. Total Faults
3. Response Time
4. Active Requests
5. Highest Concurrent Requests

Click the Home tab

6. Click Test Service.

7. Invoke each of the web service operations a few times, as you did in the testing steps 3-8 in the

Deploying and Testing the Web Service topic.


Now return to the Operations page. You should see that the Total Requests have been
incremented according to the number of invocations you made for each operation.

8. Click the Home tab. You should see that the Invocations value has been incremented to reflect

the total number of invocations for both operations.

9. Click the Administration tab, then click Enable/Disable Features.

10 In the Enable/Disable Features page, Ctrl-click to select Logging and Auditing from the
. Available Features list, then click Move to shuttle them to the Enabled Features list.

Click OK.

11 On the Administration tab, click Edit Configuration for the Auditing feature.
.

12 By default, all auditing is available for both operations.


.

Deselect the check boxes for Request Messages, Response Messages, and Fault Messages for
the getDate operation, leaving the ones for the getDateHence operation selected.
Click OK.

.
13 Again invoke each of the web service operations a few times, as you did in the testing steps 3-8
. in the Deploying and Testing the Web Service topic.

Use Windows Explorer to open for editing the file


<JDEV_HOME>\j2ee\home\log\wsmgmt\auditing\log.xml.

14 In the log file you are able to see the invocations of the web service operations (screen shots
. show Notepad with Word Wrap turned on (Format > Word Wrap):

Header information

Arguments passed to the web service

Values returned by the web service

Note that you see the invocations of the getDateHence operation only, as you specified in the
auditing options.
When you are finished examining the log file, you can close it.
Although you will not do so in this tutorial, another thing that you can do with Application
Server Control of web services is to set the inbound security policies for JAX RPC web services
deployed in OC4J container 10.1.3. You can set options such as requiring a username/password
for authentication, requiring the message body to be signed, and requiring encryption of the
message body. These administrative features can also be defined and managed from within

JDeveloper.

Back to Topic List

Monitoring
the Web Service (optional)
This topic shows you how to use the HTTP Analyzer to examine
the request/response packets sent and received by JDeveloper when you run a
proxy to a web service. When you start the HTTP Analyzer, it updates the
proxy
settings in JDeveloper so that all TCP data is sent through an intermediate
port and then on to your original proxy, if one is defined. The proxy
settings
are restored when you turn the HTTP Analyzer off, or when you exit
JDeveloper.

To monitor the web service, perform the following steps:

Create a web service proxy


Use the HTTP Analyzer
Creating
a web service proxy

1. To use the HTTP Analyzer, you need to create a web service proxy so that you can run the

service from within JDeveloper. You must create the web service proxy in a new project.
In the Application Navigator, right-click the JavaWebService application and select New
Project from the context menu.

The New Gallery opens with Empty Project highlighted -- just click OK.

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

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

In the New Gallery, expand Business Tier and select Web Services in the Categories list, then in
the Items list select Web Service Proxy and 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, invoke the WSDL Document URL
dropdown list and select the WSDL that appears in the tooltip, which is similar to the following
(although the initial part of the path is different for your computer):
file:/C:/JDeveloper/jdevstudio1013/jdev/mywork/JavaWebService/GetDates/public_html/WEBINF/wsdl/GetDatesWS.wsdl

Press the [Tab] key to validate the WSDL, then click Next.

5. On the Port Endpoints page of the wizard, select the option Run against a service deployed to

an external server. Select the Port Name and Endpoint URL that appear in the list, then click
Next.

6. Click Next on the next two wizard pages (Custom Mappings and Defined Handlers).

On the Default Mapping Options page of the wizard, change the Package Name to
datespackageproxy, then click Next.

7. Click Next on the Support Files page of the wizard.

On the Finish page you should see both methods listed. Click Finish.

The web service proxy generation process creates four directories and numerous files, and opens
datespackage.GetDatesWSSoapHttpPortClient.java in the editor (if this file does not open
automatically, double-click it in the Application Navigator to open it).

8. In the editor, locate the following line of comment in the main() method:

// Add your own code here :


Below that line, add the following code:
int addDays = 5;
System.out.println("The date and time "
+ addDays + " days hence is: " + myPort.getDateHence(addDays));

Click Make

9.

Click Run

to save and compile the file.

to run the class.

The message log displays the URL that is called on one line. On the next line it displays your
message "The future date and time: " followed by the date as a GregorianCalendar object.

Back to Topic

Using the HTTP Analyzer

1. Display the HTTP Analyzer window by selecting View > HTTP Analyzer. The Http Analyzer

window opens. By default it is docked at the lower right of JDeveloper's user interface.
Start monitoring the packets by clicking Start

in the HTTP Analyzer.

2. Re-run the class. The request/response packets are listed in the HTTP Analyzer.

3. To see the contents of a packet pair, select it in the History page, then click the Data tab.

If you have run other things with the HTTP Analyzer started, you can use the Previous
Request/Response Pair and Next Request/Response Pair buttons to examine other packet
pairs.

4. The data page shows the value that was sent to the web service in the request information (left

panels), and the value returned by the web service in the response information (right panels).

5. When you are debugging a web service, you can change the contents of a request packet and

resend it so as to see the changes in the response packet.


To try this, in the request packet that sent the value 5, change the value to 365. Then click
Resend Request .

6. The amended packet is sent. Click Next Request/Response Pair

packet to see the change.

and examine the response

7. To close HTTP Analyzer window, click the x on the Http Analyzer tab.

In this tutorial you published a Java class as a web service.


You deployed and tested it, then used Application Server Control to manage
the
web service. You also created a web service proxy for the web service and
monitored
it with the HTTP Analyzer.

Vous aimerez peut-être aussi