Skip to Content

End-to-End Weather App Scenario Part 8

By akula86

Use the Connectivity Service to access external (backend) systems.

You will learn

In this tutorial you will learn how to use the Connectivity Service to connect to an external (e.g. backend) system.


For this part of the series, you will consume a RESTful weather service that returns data in JSON format:

>Note: In order to consume the openweathermap API you need to apply for an API key as outlined here: But don’t worry, that’s easy to do and won’t take longer than two minutes!

  1. Once you have your APPID, create a new service class with the following properties:

    • Package name:
    • Classname: WeatherService
    Creating a new Java class
  2. Replace the contents of the file with this code from GitHub and save your changes.

  3. Include the full-qualified classname of the WeatherService class in the list of JAX-RS services specified in the web.xml configuration you did before. The corresponding element should now look like this:

    Modifying the web.xml file to include the new service
  4. Within the code we are obtaining a reference to an HttpDestination with the logical name openweathermap-destination via JNDI, hence we need to create that destination. For that purpose, double-click on the local server in the Servers view.

    Switch to the Connectivity tab and click on the green + symbol to add a new destination. Enter the following information (replacing YOUR_APPID with the code you received when you registered with the openweathermap service.

    • Name: openweathermap-destination
    • Type: HTTP
    • URL:
    Adding a new destination to the local web server
  5. Similar to what we have done to register the DataSource in the web.xml file, we also need to specify the HTTP destination in the web.xml file. Open it and enter the following code snippet underneath the already existing tag.

    Adding the HTTP destination to the web.xml file
  6. Save your changes and deploy/publish the application again. After successful authenticating yourself, navigate to the following URL: http://localhost:8080/weatherapp/api/v1/weather?id=2158177

    Testing the new weather destination
  7. One more thing: it would actually be nice to be able to traverse the path and query for weather information in a more RESTful manner via a URL pattern like: /api/v1/cities/{id}/weather. Let’s add a respective method to the FavoriteCityService class:

    @Produces({ MediaType.APPLICATION_JSON })
    public Response getWeatherInformation(@PathParam(value = "id") String id,    @Context SecurityContext ctx)
        WeatherService weatherService = new WeatherService();
        return weatherService.getWeatherInformation(id, null);
    Supporting a new URL by adding an @Path annotation

    Note: Since working behind corporate firewalls is a common cause of frustration I want to point out how to accommodate this. if your firewall requires a proxy for outbound communication you need to start your local server with proxy settings as follows:

    • In the Servers view, double-click the added server to open the editor.
    • Click the Open Launch Configuration link.
    • Choose the (x)=Arguments tab page.
    • In the VM Arguments box, add the following row:
      • Dhttp.proxyHost=
      • Dhttp.proxyPort=
      • Dhttps.proxyHost=
      • Dhttps.proxyPort=
    • Choose OK
    Modifying launch configuration when running behind a firewall

Next Steps

Updated 12/22/2016

Time to Complete

10 min




Back to top