【问题标题】:Eclipse 4 Application RCP and a REST APIEclipse 4 应用程序 RCP 和 REST API
【发布时间】:2013-11-11 08:12:21
【问题描述】:

我正在开发一个 Eclipse RCP 应用程序,它必须在 Treeview 中显示 DataBase 的集合。

DataBase 的集合由 REST API 提供。

所以,我要做的是通过给定 URL 和 KEY 调用 REST API 并显示结果(集合)在Treeview中。

我对 REST API 的了解是它(大部分时间)在 Web 应用程序中使用,但对我来说并非如此。

有人知道如何从 Eclipse RCP 应用程序 调用 REST API 吗? 有人使用过 RCP 和 REST API 吗?

提前致谢。

伊斯梅尔

【问题讨论】:

    标签: eclipse api rest treeview rcp


    【解决方案1】:

    您应该能够使用任何您想要的 java rest 客户端。我个人更喜欢使用 jersey,因为有时在 Eclipse 中使用 Spring Framework 会很痛苦。

    添加所有这些库对我有用:

     lib/jsr311-api-1.1.1.jar,
     lib/jersey-client-1.19.jar,
     lib/jersey-core-1.19.jar,
     lib/jersey-json-1.19.jar,
     lib/jackson-core-asl-1.9.2.jar,
     lib/jackson-jaxrs-1.9.2.jar,
     lib/jackson-mapper-asl-1.9.2.jar,
     lib/jaxb-api-2.2.2.jar,
     lib/jaxb-impl-2.2.3-1.jar,
     lib/jackson-xc-1.9.2.jar
    

    一个简单的调用如下:

    try {
                ClientConfig clientConfig = new DefaultClientConfig();
                clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
                Client client = Client.create(clientConfig);
    
                WebResource webResource = client.resource("http://localhost:8080/getMyObject");
                ClientResponse response = webResource.get(ClientResponse.class);
    
                if (response.getStatus() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
                }
    
                MyObject output = response.getEntity(MyObject.class);
    
            } catch (Exception e) {
    
                //Handling errors
    
            }
    

    JSONConfiguration.FEATURE_POJO_MAPPING 用于使用 jersey-json 和 jackson-*.jar 自动映射到 json

    【讨论】:

      【解决方案2】:

      为了调用 RestAPi,我使用了 SpringFramework :

      org.springframework.web.client.RestTemplate及其方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-13
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        相关资源
        最近更新 更多