在 XPage 中使用 REST 服务 [英] Consume REST Service in XPage

查看:53
本文介绍了在 XPage 中使用 REST 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我指点一篇关于在 XPage 中使用 REST 服务的文章、教程或演练?我见过一些使用 Domino 数据服务或 Domino REST 服务,但我希望看到一个使用外部 REST 服务,例如 PayPal.

Can anyone point me to a article, tutorial, or walk-through of getting started with consuming REST services in XPages? I have seen a few that use the Domino Data Service or a Domino REST service, but I would like to see one consuming external REST services such as PayPal's.

请不要将我引向 Social Business Toolkit,我已经看过甚至下载了它,但我觉得我不必安装 J2EE 和 Eclipse 才能看到 12 行 JavaScript 的演示.

Please don't direct me to Social Business Toolkit, I have looked at it and even downloaded it but don't feel I should have to install J2EE and Eclipse to see a demo of 12 lines of JavaScript.

推荐答案

我知道这有点事后诸葛亮,但仅仅为了在 XPage 中使用 RESTful 端点,我最近写了一篇关于在服务器上这样做的博客 -边.我的实现使用了一个 Java 类,用于通过 URLConnection 生成输出,最终使用一个 StringBuffer 来读取内容,然后将其解析为 JsonObject 以返回.我对该主题进行了两次跟进,您可以相应地找到它们:

I know this is a bit after the fact, but for mere consumption of a RESTful endpoint for use in XPages, I blogged recently about doing so on the server-side. My implementation makes use of a Java Class used to generate the output via URLConnection and, ultimately, a StringBuffer to read in the contents, then parse it into a JsonObject for return. I did two folow ups on the topic and you can find them accordingly:

系列页面/目录

  1. REST 消费,使用 Java 的服务器端
  2. 带身份验证的 REST 消费
  3. 从 Java 生成自定义 JSON 数据

我的示例使用了 Google GSON 库,但 正如 Paul T. Calhoun 所指出的,有 com.ibm.commons.util.io.json 包,它随 Domino 附带了一段时间,可能是 Domino 开发人员的更好选择(没有外部依赖项,也没有潜在的 java.policy 编辑).

My examples make use of the Google GSON library, but as pointed out by Paul T. Calhoun, there is the com.ibm.commons.util.io.json package which has come with Domino for a while and probably the better option for Domino devs (no external dependencies and no potential java.policy edit).

该方法的基本结构是:

/* 
 * @param String of the url
 * @return JsonObject containing the data from the REST response.
 * @throws IOException
 * @throws MalformedURLException
 * @throws ParseException 
 */
public static JsonObject GetMyRestData( String myUrlStr ) throws IOException, MalformedURLException {
    JsonObject myRestData = new JsonObject();
    try{

        URL myUrl = new URL(myUrlStr);
        URLConnection urlCon = myUrl.openConnection();
        urlCon.setConnectTimeout(5000);
        InputStream is = urlCon.getInputStream();
        InputStreamReader isR = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(isR);
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while( (line = reader.readLine()) != null ){
            buffer.append(line);
        }
        reader.close();
        JsonParser parser = new JsonParser();
        myRestData = (JsonObject) parser.parse(buffer.toString());

        return myRestData;

    }catch( MalformedURLException e ){
        e.printStackTrace();
        myRestData.addProperty("error", e.toString());
        return myRestData;
    }catch( IOException e ){
        e.printStackTrace();
        myRestData.addProperty("error", e.toString());
        return myRestData;
    }
}

这篇关于在 XPage 中使用 REST 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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