Skip to main content
Skip table of contents

Working with an existing web service

Learning objectives
By completing this module, you will:

  • Learn how WSDL definitions are reflected in NexJ messages.

  • Learn how to extend a class model to include a web service.

  • Reinforce your knowledge about creating a channel.

  • Reinforce or introduce some Presentation layer knowledge.

Prerequisite: The SOA service created in the previous lesson. 

Key concepts

  • WSDL is a standard XML format for describing XML web services that define the set of operations and messages that can be sent to and received from a given web service.

In this learning module, we add an attribute that gets its value from a web service call to the Person class. An input value of City is passed to the service, and a response message is returned with the temperature. This can then be optionally displayed in the user interface, demonstrating how you can quickly integrate with external non-relational data sources.

Importing the WSDL definition

To simplify interacting with the web service, we create message definitions by importing the WSDL definition. 

Learning activity

To import the WDSL definition:

  1. From the File menu, select Import.
  2. In the Import dialog, select NexJ Studio > XML Schema/WSDL and click Next.
  3. In the File paths or URLs field, enter http://localhost:7080/training/soa/training:weather:1.0/soap?wsdl and click Next.
  4. In the Message Prefix field, enter Weather_.
  5. Leave all checkboxes unchecked, and click Finish.
  6. In the Integration layer, select the Messages tab.

    A number of new messages with names that start with "Weather_" have been created. To see only these new messages, type Weather in the Search field.

  7. Double-click the Weather_getWeather message to open it and review the source. Note that the expected input is a cityName.

  8. Open the Weather_getWeatherResponse message and review the outputs that come back as a response.

Creating a new channel for integration

You are now ready to set up for accessing the web service through a channel. You'll need to create a new channel for the web service and add it to the environment.

Learning activity

To create a new channel:

  1. In the Integration layer, select the Channels tab.
  2. Right-click and select New Channel.
  3. Specify the following details:
    1. Name: training:Weather
    2. Channel Type: HTTP
    3. Click Finish.
  4. In the Source tab, enter the following code:

    <HTTP contentType="text/xml" get="true" url="http://localhost:7080/training/soa/training:weather:1.0/soap"/>

In the new channel, specify http://localhost:7080/training/soa/training:weather:1.0/soap as the URL.

  1. Set the content type as text/xml.
  2. Enable the GET verb.
  3. Save your work.

To add the channel to the environment:

  1. In the Deployment layer, select the Environments tab.
  2. Double-click the Development environment to open it for editing.
  3. In the Channel Connections tab, click the Select Channel Connections button
    . The Select Channel Connections dialog opens.
  4. Add the TrnWeather channel.
  5. Set authentication to none and the user and password to nexjsa and nexj respectively.
  6. Click OK.
  7. Save your work.

    The channel should look similar to the following:

    <HTTPConnection authentication="none" channel="training:WeatherChannel" password="text:nexj" user="nexjsa"/>
  8. Set up anonymous access to your server environment by going to the Security tab of your Development environment and enabling Anonymous RPC.

Extending the class model

Learning activity

Add an event to the Entity class that requests the information through the new channel.

  1. In the Business Model layer, select the Classes tab.
  2. Right-click the Entity class and select Customize.
  3. Double-click the Entity class to open for editing.
  4. In the Events tab, add a new event called fetchCurrentWeather.
  5. In the Actions tab for the event, add a main action.
  6. In the main action's Script tab, add the following code:

          (myCity (ifnull (@ defaultAddress city) "NA"))
          (msg (message (: :class "WeathergetWeather") (: cityName myCity)))
                (: :class "HTTP")
                (: url "http://localhost:7080/training/anon/soa/training:weather:1.0/soap")
                (: headers (message (: content-type "text/xml")))
                (: principal "nexjsa")
                (: password "nexj")
                (: body (format-message msg))
          (resp ())
          (parsed ())
          (ret "No default city found")
          (set! resp (integration-send-receive req "training:WeatherChannel" ()))
          (set! parsed (parse-message (resp'body) "WeathergetWeatherResponse"))
          (if (= myCity "NA")
             (set! ret "No location set")
             (set! ret (format "Local Temperature: {0}" ((parsed'getWeatherResult)'tempurature)))

    The code sends a message to the web service that includes the ZIP code for the entity's default address. The response is formatted when it is received.

  7. In the Entity class, go to the Attributes tab of the class editor and add a new attribute. This is a calculated attribute that contains the formatted response from the web service.
    • Name: currentWeather
    • Type: string
  8. In the Value tab for this attribute, set the value to (this'fetchCurrentWeather).
  9. Save your work.

Testing the web service integration

Write a Scheme script that creates a Person with an address, and then fetches the person's current weather through the web service.

Learning activity

To test the web service integration:

  1. Use an existing scratchpad or create a new one, and add the following code:

    (define FIRST "Tim")
    (define LAST "Lamont")
    ; Read the person
    (define thePerson (read-instance Person '() (quasiquote (and (=
    firstName ,FIRST) (= lastName ,LAST))) #f))
    ; If the person doesn't exist, create it
    (if (null? thePerson)
    		(set! thePerson (Person'createEntity "Contact" (: firstName FIRST) (:
    lastName LAST)))
    			(read-instance AddressType '() '(= name "Home") #f)
    			(: address1 "Lamont Tower")
    			(: address2 "1997  Orphan Road")
    			(: city "Toronto")
    			(: state "ON")
    			(: country "CA")
    			(: zip "M4A E3T")
    ; Fetch the person's weather
  2. Save the scratchpad.
  3. Run the Server Console.

  4. Execute each statement of the scratchpad in the Server Console and review the results.

  5. Stop the Console.

Testing the updated application

Learning activity

To test the updated application:

  1. Start your server.
  2. Load http://localhost:7080/training/ui/portal in your browser.
  3. Navigate to the Contacts workspace.

The weather information should appear in the Contact Navigator.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.