< Previous | Next >

Lesson 2: Creating the web service

In this lesson you will learn how to create the top-down Java™ address book web service from the WSDL file imported in the previous lesson.

Before you begin

Before you begin, you must complete Lesson 1: Creating a server and web project.

About this task

Procedure

  1. Switch to the Java EE perspective: Window > Open Perspective >Other > Java EE.
  2. In the Enterprise Explorer view, select the WSDL file you imported in the previous lesson.
  3. Right-click the WSDL and select Web Services > Generate Java bean skeleton.
  4. Web Services page: select Top down Java bean Web service as your web service type and ensure the AddressBook.wsdl is set as the service definition. Choose the following options:
    1. Select the stages of web services development that you want to complete using the slider. The slider sets the defaults on the remaining wizard pages, but you can override the default settings on each page as you proceed. For this tutorial select Start service. This will create all the code required for the web service, deploy it to the server and attempt to start the server.
    2. Select your server: click the server link and ensure that the WebSphere® v8 Server is selected.
    3. Select your runtime: ensure the IBM® JAX-WS runtime is selected.
    4. Select the service project: Select the jwsAddressBook project created in the previous lesson.
    5. Select the service EAR project: Select the jwsAddressBookEAR created in the previous lesson.
    6. The client will be created later, so ensure the client slider is set to No client.
    Click Next.
  5. WebSphere JAX-WS Top Down Web Service Configuration page:
    • Output folder: Accept the default location where the generated Java skeleton will be generated: jwsAddressBook/src.
    • Target package: Accept the default package name
    • Enable wrapper style: Enables wrapper style mapping from WSDL to Java. For WSDL documents that implement a document/literal wrapped pattern, a root element is declared in the XML schema and is used as an operation wrapper for a message flow. Separate wrapper element definitions exist for both the request and the response. Accept the default.
    • Enable MTOM support: If you select this check box the SOAP Message Transmission Optimization Mechanism will be enabled to optimize the transmission of binary content. Accept the default.
    • Version of JAX-WS code to be generated: Starting with WebSphere Application Server v8.0, you can generate JAX-WS 2.1 compliant code. Select 2.1 for this tutorial.
    • Copy WSDL to project: Select this to copy the WSDL file into the service project. Since you will create the client at a later time select this check box.
    • Generate serializable JAXB classes: In WebSphere Application Server v7.0 and up, when you enable the Java 6 facet, you can choose to generate JAXB classes which implement java.io.Serializable. Classes that do not implement this interface will not have any of their state serialized or deserialized. Do not select this for the tutorial.
    • Specify JAX-WS or JAXB binding files: this allows you to use JAX-WS or JAXB custom binding files. Do not select this for the tutorial.
    • Customize service implementation class name: this allows you to change the default port name to service implementation class name mapping. Do not select this for the tutorial.
    • Generate schema library: Selecting this will run the JAX-WS Schema to Java compiler to generate a schema. Do not select this option for this tutorial.
    • Generate Web service deployment descriptor: For JAX-WS web services deployment information is generated dynamically by the runtime; static deployment descriptors are no longer required. Selecting this check box will generate them. This tutorial does not require deployment descriptors.
    Click Next.

Results

All the required code for the web service is generated

Adding the business logic to the skeleton bean

About this task

The skeleton implementation bean generated by the web service wizard AddressBookPortImpl.java does not contain any business logic. It does contain the annotation @javax.jws.WebService which tells the runtime that it is a JAX-WS web service.

In order to make the address book web service function as expected, you need to add code to this bean. It should be opened in an editor automatically after generating the web service, but if not it can be found in: jwsAddressBook/Java Resources/src/com.addressbook.

Procedure

  1. Replace the current saveAddress method:
        public boolean saveAddress(PersonType person) {
            return false;
        }
    with the following static field and method:
    	private static Hashtable<String,AddressType> addresses = new Hashtable<String,AddressType>();
        public boolean saveAddress(PersonType person) {
        	addresses.put(person.getName(),person.getAddress());
            return true;
        }
  2. Replace the current findAddress method:
        public AddressType findAddress(String name) throws FindAddressFault {
            return null;
        }
    with the following:
        public AddressType findAddress(String name) throws FindAddressFault {
            return addresses.get(name);
        }
  3. Several error markers may be displayed. To correct these, organize your imports by clicking Ctrl+Shift+o. Select to import java.util.Hashtable. After this is done the errors should be grayed out.
  4. Save the updated implementation bean.

Test the web service using the Generic Service Client

About this task

The Generic Service Client allows you to test a web service without building a client. You can select the operation you want to test, enter the required information, and the result will display in the Status pane.

Procedure

  1. Select the generated WSDL file jwsAddressBook/WebContent/WEB-INF/wsdl/AddressBook.wsdl, right-click and select Web Services > Test with Generic Service Client. Alternatively you can select the service under the project's Services node or the JAX-WS web services node in the Services view, and launch the Generic Service Client from there.
  2. Select the SaveAddress operation.
  3. Enter values in each of the fields and click Invoke.
  4. Select the FindAddress operation.
  5. Enter the name you chose when invoking the saveAddress operation and click Invoke.
  6. The information you saved to this name will display in the Status pane.

Lesson Checkpoint

Now you are ready to begin Lesson 3: Creating the web service client.

< Previous | Next >
Icon that indicates the type of topic Tutorial lesson topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: jaxwstd_exercise12.html