Create a Web Service from a Java Class

Web services consist of a set of messaging protocols and programming standards that expose business functions over the Internet using open standards. A web service is a discrete, reusable software component that is accessed programmatically over the Internet to return a response. You can work in JDeveloper's IDE to quickly create a web service from a Java class. You can then test the web service by running it on JDeveloper's integrated server, and running a generated proxy against the web service.

Create a New Application: Use the Create Application wizard to create a generic application.

Create a Java Class: Create a Java class containing a method that can be exposed as an operation in a web service.

Create the Web Service: Use the Create Java Web Service wizard to publish the Java class and expose the method as a web service operation.

Use the HTTP Analyzer to Test the Web Service: Run the web service in JDeveloper's integrated server instance and test the service in the HTTP Analyzer.

Create a Proxy to the Web Service: Use the Create Web Service Proxy wizard to generate a proxy to the web service.

Add Code to Call the Proxy: In the source editor, add code to the main method in the generated Java client. Then run the Java client.

 

Create a New Application and Project

tell me more icon Creating a New Application and Project

The JDeveloper application is the highest level in the organizational structure. It stores information about the objects you are working with, while you are creating your application. It keeps track of your projects and the environment settings while you are developing.   [ tell me more...]





  1. Open the Create Application wizard [ File > New > General > Applications] .
  2. To follow along with the example, enter the application name ServiceFromClass.
  3. Select Generic Application in the Applica tion Template list, and click Finish .

    tell me more icon Application Templates

    Application templates provide you with a quick way to create the project structure for standard applications with the appropriate combination of technologies already specified. The new application created from the template appears in the Application Navigator already partitioned into tiered projects, with the associated technology scopes set in each project.   [ tell me more...]




    tell me more icon In the IDE

    When you complete the steps for creating a new application and project, the Projects panel in the Application Navigator should look like this:  [ tell me more...]



 

Create a Java Class

tell me more icon Creating a Java Class

New classes can be created and added to your project after you build the initial application and project.   [ tell me more...]





  1. In the Application Navigator, select the project you just created and open the Create Java Class dialog [ File > New > General > Simple Files > Java Class] .
  2. Enter CreditRating as the class name, and ratingservice as the package name. Accept the remaining defaults and click OK.

    tell me more icon Java Class Name and Package

    To follow along with the example, in this step and throughout the cards, enter values as shown in the instructions. Then you will be able to follow the steps in the cue cards exactly as written.   [ tell me more...]



  3. In the source editor, add a method that will be exposed as an operation in the web service.
    code sample icon Use sample code

    tell me more icon Using Java Source Editor Features

    The insert or replace mode for the editor is controlled by the Insert key on the keyboard. The default is insert mode. Press Insert to toggle between insert and replace mode.  [ tell me more...]



  4. Click the save all icon Save All to save your work.

    tell me more icon In the IDE

    When you complete the steps for creating a Java class, the Application Navigator should look like this:  [ tell me more...]



 

Create the Web Service

tell me more icon Creating the Web Service

You can quickly create a web service to expose the method in the Java class as an operation in the service.   [ tell me more...]





  1. In the Application Navigator, right-click the java node icon CreditRating.java node and choose Cr eate Web Service to launch the Create Java Web Service wizard. If the Welcome page displays, click Next .
  2. On step 2 of the wizard, Select Deployment Platform, select Java EE 1.5, with support for JAX-WS Annotations. Click Next .

    tell me more icon Selecting the Deployment Platform

    When you use the wizard to create the first Java web service in a project, you have to select the Java EE version using the Select Deployment Platform page, which looks like this:  [ tell me more...]



  3. On step 3, Generation Options, make sure ratingservice.CreditRating is shown in the Componen t to Publish box.

    tell me more icon Publishing the Java Class

    When you launch the wizard from the context menu on the Java class file in the Application Navigator, JDeveloper automatically populates the Component To Publish field with the fully qualified class name.   [ tell me more...]



  4. Accept the default values for the web service name and port name. Click Next .
  5. On step 4, Message Format, select SOAP 1. 2 Binding , then select Document/Literal from the SOAP Message Fo rmat dropdown list. Click Next .
  6. On step 5, Methods, make sure the processRating method is selected. Click Finish .

    tell me more icon Exposing Methods as Web Service Operations

    When you launch the Create Java Web Service wizard from the context menu on the Java class file in the Application Navigator, JDeveloper automatically selects all available methods in the Java class for exposing as operations in the web service.  [ tell me more...]



  7. Click the save all icon Save All to save your work.

    tell me more icon In the IDE

    When you complete the steps for creating the web service using the wizard, JDeveloper displays some messages in the Web Services Log window.  [ tell me more...]



 

Test the Web Service Using the HTTP Analyzer

tell me more icon Testing the Web Service Using the HTTP Analyzer

You can quickly test the web service using JDeveloper's integrated server. To begin testing, you will right-click the web service in the Application Navigator, and choose Test Web Service from the context menu.  [ tell me more...]





  1. In the Application Navigator, right-click the webservice icon CreditRating.java node and choose Test Web Service .

    tell me more icon Testing the Web Service in Integrated WebLogic Server

    After you create a web service, you will usually test it to ensure that you get the results you expect. JDeveloper's IDE allows you to deploy the web service to an integrated server for testing.   [ tell me more...]



  2. In the HTTP Analyzer test window, enter 123456789 in the parameter field. Then click the send request icon Send Request. You should see 1 in the Response pane.

    tell me more icon HTTP Analyzer

    The HTTP Analyzer allows you to examine and monitor HTTP request and response packets. When you select Test Web Service, by default JDeveloper displays the HTTP Analyzer log window and HTTP Analyzer Instances window below the editor window. The web service deploys to Integrated WebLogic Server, then JDeveloper displays the HTTP Analyzer test window with the Request and Response panes placed side by side.   [ tell me more...]



  3. Take note of the WSDL URL in the HTTP Analyzer test window. Do not stop the application, Integrated WebLogic Server or close the analyzer windows. You need the web service to be running when you test a client against a web service proxy later.

    tell me more icon In the IDE

    After completing the steps for testing the web service in JDeveloper's integrated server instance, the HTTP Analyzer test window in the IDE should look similar to this:   [ tell me more...]



 

Create a Web Service Proxy

tell me more icon Creating a Web Service Proxy

JDeveloper makes it easy to use a web service in an application by allowing you to create a proxy to the service with the Create Web Service Proxy wizard. A web service proxy lets you access the web service from a client application.  [ tell me more...]





  1. Launch the Create Generic Project wizard [ File > New > General > Projects > Generic Project]  to create a new generic project. Enter RatingProxy as the project name and click Finish .
  2. In the Application Navigator, select the project you just created and launch the Create Web Service Proxy wizard [ File > New > Business Tier > Web Services > Web Service Proxy] . On the Welcome page of the wizard, click Next .

    tell me more icon Web Service Proxy

    JDeveloper creates a web service proxy class from a web service description (WSDL), which is a description language that defines how to communicate with a web service.  [ tell me more...]



  3. On step 2, Select Client Style, select JAX-WS Style. Click Next .
  4. On step 3, Select Web Service Description, in the WSD L Document URL field, enter the WSDL URL that you noted from the main analyzer window in the previous cue card. Then click Next .

    tell me more icon Selecting a Web Service Description

    There are two ways you can reference the web service description (WSDL): URL or existing web service description document file.   [ tell me more...]



  5. On step 4, Specify Default Mapping Options, enter ratingproxy.proxy as the package name and ratingproxy.proxy.types as the root package name. Click Next .
  6. On step 5, Port Endpoints, accept the default values, then click Finish to end the wizard.
  7. Click the save all icon Save All to save your work.

    tell me more icon In the IDE

    After completing the steps for creating a web service proxy from a web service description, JDeveloper displays the Java client file in the source editor. The Application Navigator should look similar to this:   [ tell me more...]



 

Create a Java Client to Invoke the Web Service Proxy

tell me more icon Creating a Java Client to Invoke the Web Service Proxy

In the previous cue card, when you completed the steps for creating a web service proxy from a web service description, JDeveloper generated the Java client file along with other web service proxy files.   [ tell me more...]





  1. If it is not already open, double-click java node icon CreditRatingSoap12HttpPortClient.java in the Application Navigator to open it in the source editor.
  2. In the editor, add code in the main method to call the desired method.
    code sample icon Use sample code

    tell me more icon Java Client

    When you used the wizard to generate the web service proxy in the previous card, JDeveloper also automatically generated the CreditRatingSoap12HttpPortClient.java file, which is a Java client that can connect to the proxy.   [ tell me more...]



  3. Click the save all icon Save All to save your work.
  4. In the Application Navigator, right-click the java node icon CreditRatingSoap12HttpPortClient.java node and choose R un .

    tell me more icon Running the Java Client

    Before you can test the Java client to the web service proxy, the web service must be up and running to process the client request.   [ tell me more...]




    tell me more icon In the IDE

    When you run the Java client, JDeveloper should display the response value 1 in the RatingProxy.jpr Running Log window:  [ tell me more...]



 













Copyright © 1997, 2009, Oracle. All rights reserved.

false ,,,,,,,,,,,,,,,