WS with JDev and OC4J

Last modified by superadmin on 2018-01-12 20:27

WS with JDev and OC4J

This exercise develops a Web Service "StockQuoteService" from a POJO (ordinary or "Plain Old" Java object) with JDeveloper11. JDeveloper11 is still a technology preview (the official release is still JDeveloper/OC4J v10), but the version11 supports the JAX-WS specification. After that the service is deployed to OC4J and tested. Stateless nature of the Web Service is observed. 

Create a Web Service "StockQuoteService"

  • Open the JDeveloper11 application: K -> Development -> JDeveloper11
  • Pick New Application and choose the Application Name value "WebService".
  • Create a project called "Annotation". 
  • Create a New Java Class "HelloService" in the project "Annotation" and package "annotation". 
  • Type this code in the "StockQuoteService" (or copy-paste from here): 
package annotation;

public class StockQuoteService {
    private Map<String,Double> map
      = new HashMap<String,Double>();
   
    public StockQuoteService() {    
    }
   
    public double getPrice(String symbol) {
        Double price = map.get(symbol);
        if (price != null) {
            return price;
        }
        return 42.0;
    }
   
    public void update(String symbol, double price) {
        map.put(symbol, price);
    }
   
}
  • Press Alt+Enter to add imports for Map (java.util.Map) and HashMap (java.util.HashMap). 
  • Add annotation @WebService before the "public class StockQuoteService" line
  • Click on the red bulb and select Configure project for web services
  • Select "Java EE 1.5 with support for JAX-WS Annotations" in the wizard
  • Now the project "Annotation" has "Web Content" sub-directory; you can inspect its contents and file "web.xml". It should contain one "servlet" element and one "servlet-mapping" element. 
  • Add annotations @WebMethod in front of both public methods "getPrice()" and "update()". The code should look like this: 
package annotation;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebService;


@WebService
public class StockQuoteService {
    private Map<String,Double> map
      = new HashMap<String,Double>();
   
    public StockQuoteService() {    
    }
   
    @WebMethod
    public double getPrice(String symbol) {
        Double price = map.get(symbol);
        if (price != null) {
            return price;
        }
        return 42.0;
    }
   
    @WebMethod
    public void update(String symbol, double price) {
        map.put(symbol, price);
    }
   
}
  • Select from the menu View -> Property Inspector, and click at the "StockQuoteService.java" and open the "Source" tab. After that open the Web Services tab in the property inspector. Look at the "name", "Service name" and "Port name" of the service.
  • You can test the web service on the embedded OC4J container by right-click the class "StockQuoteService.java" and select "Test Web Service". 
  • In the console there is a URL address displayed e.g. http://localhost:8988/WebService-Annotation-context-root/stockquoteserviceport##; copy this address to the clipboard. 
  • Open Firefox browser and paste the address to the browser, and add "?wsdl" at the end of this address. Go to that address. A WSDL file should open in the browser. You can download this file and save to some temporary location (give it extension ".wsdl"). 
  • Pick method "update()"; select symbol=ACN and price=100.0. Submit the data. The method should succeed. 
  • Pick method "getPrice()"; select symbol=ACN. Observe that the returned price is still 42.0. I.e. the Web Service does not preserve the state (i.e. the value is recorded in the map, but at the next call it is lost, because a new instance of the object is created). 
  • (If you have difficulty running the "HTTP Analyzer", then press the "Open WSDL" button and browse the WSDL file you saved earlier.)

Deploy the service to OC4J

  • Shut down the embedded OC4J container - pick from the JDeveloper menu Run -> Terminate -> Embedded OC4J Container
  • Open a command console and type "oc4j -start". The OC4J console window should appear. Do not close this window, since you will terminate the OC4J server. (To shutdown the OC4J, type Ctrl+C in this window and let it finish). 
  • Select from JDeveloper menu View -> Resource Palette
  • In the Palette window, click on the folder with a plus sign and select New Connection -> "Application Server*. 
  • Leave the default connection name - "AppServerConnection1" and "Standalone OC4J 11g", press "Next"
  • Enter the username=oc4jadmin and password=Student007, press "Next"
  • Leave the default host (localhost) and default RMI port (23791) and press next. 
  • Press button "Test Connection"; you should see success. 
  • Right-click the "Annotation" project and pick "Deploy"->"WebServices"->"to"->"AppServerConnection1". 
  • Read the "Deployment Plan" and press "OK". 
  • See that OC4J console prints a few diagnostic lines. 
  • If there are no error messages (Java exception stacks), the service is now deployed. 

Test the service on Standalone OC4J

  • Open Firefox and paste the URL address that was displayed for the "embedded test" (e.g. http://localhost:8988/WebService-Annotation-context-root/stockquoteserviceport##). Change the port number from 8988 (embedded) to the 8888 (standalone). Also add "?wsdl". Open the WSDL file. You should see almost the same result as for the embedded testing. 
  • Open the "HTTP Analyzer", press the second button from the left (the blue square), i.e. "Create New Request". 
  • Press the Button "Open WSDL" and paste the address you used in the Firefox browser, i.e. with port 8888, but now remove the "?wsdl". Press "OK". 
  • Pick the service (update) and set the stock price for symbol "ACN". 
  • Later make yet another request, use the service (getPrice) and check that the stock price was forgotten. 

Bibliography

  1. http://www.oracle.com/technology/obe/obe11jdev/11/ws/ws.html - JDeveloper11 Web Service tutorial.
Tags:
Created by Kalvis Apsītis on 2007-10-29 14:08
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation