创建 REST/JSON API [英] Creating REST/JSON APIs

查看:33
本文介绍了创建 REST/JSON API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是 REST Web 服务的新手,所以我想问一些关于 REST API 的简单问题.我创建了一个 Java 应用程序,它使用以下方法通过 REST 提供数据:

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)+")";
}

我为客户端应用程序创建了以下方法来获取 JSON 对象并将其反序列化为 LinkedHashMap:

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;

}

如果我想创建一个 API 以便为不同语言的应用程序提供此服务,我应该怎么做?只提供包含在 getDataClient 中的 URL?我很困扰.如果有人能给我一个解释(或一个小例子),我将不胜感激.谢谢!

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!

推荐答案

如果我想创建一个 API 来提供这个服务不同语言的应用,具体应该怎么做?

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

Web 服务的主要目的之一是允许异构(不同技术)系统之间进行通信.REST 服务建立在 HTTP 协议之上,因此任何支持 HTTP 通信的客户端技术都可以使用您的 REST 服务.

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.

只提供包含在 getDataClient 中的 URL?

Just provide the URL included in the getDataClient?

URL 用于标识每个实体,但您可能还必须提供其他信息,例如:输入参数详细信息、所需标题等.最好编写一个小规范或您的 REST API 使用指南,以帮助客户轻松无缝地使用您的服务.

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.

这篇关于创建 REST/JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆