As I am a novice to REST web services, I would like to ask something simple about REST APIs. I have created a Java application that provides data via REST with the following method:
@RequestMapping(value = "/JSON/ReceiveData/{metricOne}/{metricTwo}")
public @ResponseBody
String getData(@RequestParam("callback") String callback, @PathVariable String metricType,
@PathVariable String metricPeriod) {
LinkedHashMap<String,String> map = new LinkedHashMap<String, String>();
try{
map = service.getData(metricOne, metricTwo);
}catch(NullPointerException e){
e.printStackTrace();
}
return callback+"("+t2JsonUtil.toJsonString(map)+")";
}
I have created the following method for the client application to obtain and deserialize into a LinkedHashMap the JSON object:
public LinkedHashMap getDataClient(String metricOne, String metricTwo) {
LinkedHashMap<String,String> map = null;
try {
URL url = new URL("http://localhost:8081/Metrics/Stats/JSON/ReceiveData/"+metricOne+"/"+metricTwo+"/?callback=");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output = br.readLine();
output = output.substring(1,output.length()-1);
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
TypeReference<LinkedHashMap<String,String>> typeRef= new TypeReference<LinkedHashMap<String,String>>() {};
map = mapper.readValue(output, typeRef);
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
If I want to create an API in order to offer this service to applications of different languages, what should I do exactly? Just provide the URL included in the getDataClient? I am very confused. I would be very grateful if someone could give me a explanation (or a small example) about this. Thanks!