Training

WLSKB007 - Creating a Web Service in WebLogic Workshop Tutorial

Popular Courses

Browse Our Free Resources

  • whitepapers
  • whitepapers
  • webinars
  • blogs

Our Locations

Training Centres

Vancouver, BC
Calgary, AB
Edmonton, AB
Toronto, ON
Ottawa, ON
Montreal, QC
Hunt Valley
Columbia

locations map

Calgary

550 6th Av SW
Suite 475
Calgary, AB
T2P 0S2

Toronto

821A Bloor Street West
Toronto, ON
M6G 1M1

Vancouver

409 Granville St
Suite 902
Vancouver, BC
V6C 1T2

U.S. Office

436 York Road
Suite 1
Jenkintown, PA
19046

Other Locations

Dallas, TX
Miami, FL

Web Age Solutions Inc.

Introduction

At this point (Workshop 8.1), you can create a Web Service from bottom up. That is, you can create a service implementation and then generate the WSDL from it. You can not create a new service based on some one else's WSDL file.

Operations available from a Web Service can be invoked from a Page Flow. To do so, we need to create a Web Service Control in the project where the Page Flow is being developed. (The Web Service itself can be developed in another application or in another system - in WebSphere for example). The Web Service Control acts as a client side proxy. It makes the process of invoking a service operation as easy as calling a Java method.

In this tutorial, we will develop a service that can authenticate a user.

Before You Get Started

This tutorial depends on WLSKB002. Make sure that you have successfully completed that tutorial. Open the BasicApp application in Workshop unless you already have it open.

Create the Web Service

First, we will create a plain Java class that will serve as the input and output for the Web Service.

Right click on the BasicAppWeb project and select New->Folder. Enter MyWebServices as the folder name. Right click on the MyWebServices folder and select New->Java Class. Set the name of the class to AuthInfo.java and click on Create. Add the following member variables to the class.

 public String userId;
 public String password;
 public int resultCode;
 public String message;

Save and close the file.

Right click on MyWebServices and select New->Web Service. Enter AuthenticationService.jws as the file name. Click on Create.

Right click on the service and select Add Method. Hit Enter. Click on the newMethod1 hyperlink. Note the @common:operation XDoclet tag above the method indicating that this method is one of the operations offered by this service.

Change the method signature to as follows.

public AuthInfo authenticateUser(AuthInfo info)

Add the following implementation code.

public AuthInfo authenticateUser(AuthInfo info)
{
    if (info.userId.equals("bob") && info.password.equals("bobcat")) {
        info.resultCode = 1;
    } else {
        info.resultCode = 0;
        info.message = "Service Error:: Invalid user ID or password";
    }
 
    return info;
}

Save and close the file.

Unit Test the Service

A service is unit tested in the project where it is developed. From the menubar, select Debug->Start.

Click on the Test XML tab.

In the SOAP body text area, set the <userId> tag value to bob and <password> tag value to bobcat. Click on the authenticateUser button.

In the result page, scroll down and locate the Service Response section. Make sure that the resultCode value is 1.

Scroll up. Click on the Test Operations link. Try other user ID and password values.

Use the Service in a Page Flow

A Page Flow can act as a consumer of a Web Service. The Page Flow may be developed in a separate application than where the Web Service is developed. To keep things simple, we will use the LoginFlow Page Flow.

A consumer accesses a Web Service through a Web Service Control. We need the WSDL file for the service to generate the control's code. Right click on AuthenticationService.jws and select Generate WSDL File. System will generate AuthenticationServiceContract.wsdl.

Right click on the LoginFlow folder and select New->Java Control.

Select Web Service. Enter AuthServiceControl as the file name. Click on Next. Use the Browse button to locate the MyWebServices/AuthenticationServiceContract.wsdl file. Click on Create. System will create AuthServiceControl.jcx and open it in an editor. Click on the Source View tab at the bottom of the editor. Notice, how system has added the AuthInfo inner class. Close the editor.

Double click on LoginFlowController.jpf to open the Page Flow in the editor. Click on the Action View tab at the bottom of the editor. Drag AuthServiceControl.jcx and drop it on the editor. Click on the Source View tab. Notice, how the system has added a control member variable as shown below.

/**
 * @common:control
 */
private LoginFlow.AuthServiceControl authServiceControl;

Change the implementation of the Login() action method as follows.

protected Forward Login(LoginFormBean form)
{
    String forward_name = "";
    String message = "";
 
    AuthServiceControl.AuthInfo info = new AuthServiceControl.AuthInfo();
 
    info.userId = form.getUserId();
    info.password = form.getPassword();
 
    info = authServiceControl.authenticateUser(info);
 
    if (info.resultCode == 1) {
        forward_name = "success";
        getRequest().getSession().setAttribute("userId", form.getUserId());
    } else {
        forward_name = "failed";
        message = info.message; 
    }
    //Store the message in the request context
    getRequest().setAttribute("message", message);
   
    return new Forward(forward_name);
}

Save changes.

Test

Open a web browser. Enter the URL: http://localhost:7001/BasicAppWeb/LoginFlow/LoginFlowController.jpf

Make sure that you can only login as bob with password bobcat. Try creating an error condition.

Conclusion

A few things to remember from this tutorial.

  1. A Web Service is implemented in Workshop from a Java class with .jws extension.
  2. A public method within this can be adorned with the @common:operation XDoclet tag to make that method an operation offered by the service.
  3. A Web Service can be (and should be) unit tested before developing a consumer.
  4. The process of developing a consumer is same if the wevice was developed in another project or another system such as .Net or WebSphere.

Feedback


Email Address: *


Very useful Somewhat useful Not bad Needs many corrections


Comments:
*


( * ) = mandatory field