Skip to Content
Previous

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.

Details

For this part of the series, you will consume a RESTful weather service that returns data in JSON format: http://openweathermap.org/api.

>Note: In order to consume the openweathermap API you need to apply for an API key as outlined here: http://openweathermap.org/appid. 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: com.sap.hana.cloud.samples.weatherapp.api
    • Classname: WeatherService
    Creating a new Java class
  2. Replace the contents of the WeatherService.java 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:

    <init-param>
    	<param-name>jaxrs.serviceClasses</param-name>
       	<param-value>
       		com.sap.hana.cloud.samples.weatherapp.api.AuthenticationService,
    		com.sap.hana.cloud.samples.weatherapp.api.FavoriteCityService,
    		com.sap.hana.cloud.samples.weatherapp.api.WeatherService
    	</param-value>
    </init-param>
    
    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: http://api.openweathermap.org/data/2.5/weather?APPID=YOUR_APPID
    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.

    <resource-ref>
     	<res-ref-name>openweathermap-destination</res-ref-name>
     	<res-type>com.sap.core.connectivity.api.http.HttpDestination</res-type>
    </resource-ref>
    
    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:

    @GET
    @Path("/{id}/weather")
    @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/08/2016

Time to Complete

10 min

Intermediate

Tags

Prerequisites

Next
Back to top