Anto
Anto

Reputation: 4305

Creating REST/JSON APIs

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!

Upvotes: 1

Views: 1722

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

If I want to create an API in order to offer this service to applications of different languages, what should I do exactly?

One of the main purpose of web services is to allow communication between heterogeneous(different tehnologies) systems. REST services are build on HTTP protocol so any client technology which supports HTTP communication can consume your REST service.

Just provide the URL included in the getDataClient?

URL is to identify each entity but you may have to provide other info such as: input parameters details, required headers etc as well. Better write a small specification or your REST API usage guide to help the clients to consume your services easily and seamlessly.

Upvotes: 3

Related Questions