Sunday, August 17, 2014

How to use "Uri-template" with WSO2 API Manager

In this post I am going to develop a small use case which has used the "Uri-template " to find the correct resources

Pre-requires

WSO2 Developer studio - (version 3.5.0 or later)
WSO2 API Manager - (version 1.5.0 or later)
WSO2 Application server - (version 5.2.0 or later)

Contents
  1. Create a sample production service called Temperature-Reporter(TR).
  2. Host the TR service in the Application server.
  3. Create & Publish the API for TR service 
  4. Subscribe to the API
  5. Invoke the TR service via API (using curl ).

Create sample production service called Temperature-Reporter(TR).

Start the Developer Studio and go the dashboard. Create a JAX-RS service called TR ,this will the our production service , this a REST service. This service provides the average temperature of major cities in the world during the major seasons. As a example NYC average temperature  in the march is 4.5 in Celsius. likewise users will be able to check the avg temp by providing the city name. This is service class for the TR production service

1:  package com.blogspot.jasyz.tr.service;  
2:  import javax.ws.rs.*;  
3:  import javax.ws.rs.core.Response;  
4:  import javax.ws.rs.core.MediaType;  
5:  @Path("/temp")  
6:  public class TemperatureReporter {  
7:        @GET  
8:        @Path("{type}")  
9:        @Produces(MediaType.TEXT_PLAIN)  
10:        public Response getTempature(@PathParam("type") String type,  
11:                  @QueryParam("city") String city,  
12:                  @QueryParam("month") String month){  
13:             double temp=0.0;  
14:             if("NYC".equals(city)){  
15:                 if("jan".equals(month)){  
16:                       if("cel".equals(type)){  
17:                                temp = (4.0-3.0)/2.0;  
18:                           }else{  
19:                                temp =(39.0+26.0)/2.0;  
20:                           }  
21:                 }else if("feb".equals(month)){  
22:                      if("cel".equals(type)){  
23:                           temp = (5.0-3.0)/2.0;  
24:                      }else{  
25:                           temp =(40.0+27.0)/2.0;  
26:                      }  
27:                 }else if("mar".equals(month)){  
28:                      if("cel".equals(type)){  
29:                           temp = (8.0-1.0)/2.0;  
30:                      }else{  
31:                           temp =(48.0+34.0)/2.0;  
32:                      }  
33:                 }   
34:                 // Continue the logic   
35:             }else{  
36:                  return Response.status(200).entity("Sorry, Currently Information not avilable").build();  
37:             }  
38:            return Response.status(200).entity("tempareture of the "+city+" is "+temp+" in "+type).build();  
39:        }  
40:  }  

Now , right click on the project and export the project  then war file[1] will be created.

Host the TR service in the Application server

Start WSO2 Application Server , select the add jax RS/jax WS  menu from the left side bar. Then upload the previously created war file. Then if you check the server console you will see the following message in the server console.




 Try out the Raw TR service 

Before create the API lets try out the TR service directly , I am going to try out this service using curl , Open the terminal and paste the following command.(Curl is already installed)

 curl -v "http://10.100.1.102:9764/TR_1.0.0/services/temperature_reporter/temp/cel?month=Jan&city=NYC"  

result will be

  temperature of the NYC is 0.5 in cel  

Create & Publish the API 

Start the WSO2 API Manager and login to the publisher portal. Create the API by entering the following details




No comments:

Post a Comment