在XPage中使用REST服务

Joh*_*ohn 4 rest web-services xpages xpages-extlib

任何人都可以指向我开始在XPage中使用REST服务的文章,教程或演练吗?我见过一些使用Domino数据服务或Domino REST服务的人,但我希望看到一个消费外部REST服务,如PayPal.

请不要指导我使用社交商务工具包,我已经查看了它,甚至已经下载了它,但我觉得我不应该安装J2EE和Eclipse来查看12行JavaScript的演示.

Eri*_*ick 8

我知道这有点事后,但仅仅消耗了一个用于XPage的RESTful端点,我最近在博客上写了关于在服务器端这样做的事情.我的实现使用了一个Java类,用于通过URLConnection生成输出,最后是一个StringBuffer来读取内容,然后将其解析为JsonObject以便返回.我对这个话题进行了两次跟进,你可以找到它们:

系列页面/ TOC

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

我的例子利用谷歌GSON库,但作为由Paul T.卡尔霍恩指出,有com.ibm.commons.util.io.json包已配备了多米诺了一段时间,可能是更好的选择适用于Domino devs(没有外部依赖项,没有潜在的java.policy编辑).

该方法的基本结构是:

/* 
 * @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;
    }
}
Run Code Online (Sandbox Code Playgroud)