【问题标题】:How can I use spring RestTemplate to make a test request against the Cloudant API?如何使用 spring RestTemplate 对 Cloudant API 发出测试请求?
【发布时间】:2015-06-05 13:02:32
【问题描述】:

我正在寻找使用 Spring RestTemplate 对 Cloudant 执行基本 API 调用的一些指导。

以下是一些假设:

但是,我不确定如何开始将 RestTemplate 与 Cloudant 一起使用。例如,是否有一个基本的 Cloudant API 调用可用于测试连接并返回结果集?

【问题讨论】:

  • downvoters:如果您对这个问题投了反对票,请告诉我原因,以便我改进问题。
  • 如果您知道如何使用组件,您想要什么“基本指南”?你有什么问题?
  • 感谢您的反馈。希望现在问题更清楚了吗?
  • 更好,但仍不清楚。如果您要询问如何向 API 发出测试请求,请具体询问。

标签: resttemplate cloudant


【解决方案1】:

以下示例向您展示了如何使用 Cloudant Education 的示例数据库对 Cloudant 进行基本调用。

import org.springframework.web.client.RestTemplate;

public class Application {

    private final static String URL = 
        "https://education.cloudant.com/animaldb/_design/views101/_view/latin_name?include_docs=true";

    public static void main(String args[]) {

        RestTemplate restTemplate = new RestTemplate();

        // Normally you would bind the response to Java domain objects and not String
        // See https://spring.io/guides/gs/consuming-rest/ for examples of binding 
        // response objects to Java domain objects

        String animals = restTemplate.getForObject(URL, String.class);

        System.out.println(animals);
    }
}

你应该会看到一些类似这样的输出:

...
{
  "total_rows": 5,
  "offset": 0,
  "rows": [
    {
      "id": "kookaburra",
      "key": "Dacelo novaeguineae",
      "value": 19,
      "doc": {
        "_id": "kookaburra",
        "_rev": "4-6038cf35dfe1211f85484dec951142c7",
        "min_length": 0.28,
        "max_length": 0.42,
        "wiki_page": "http:\/\/en.wikipedia.org\/wiki\/Kookaburra",
        "class": "bird",
        "diet": "carnivore",
        "latin_name": "Dacelo novaeguineae"
      }
    },
    ... 
  ]
}
...

注意

【讨论】:

    猜你喜欢
    • 2018-01-06
    • 1970-01-01
    • 2016-05-29
    • 2016-11-25
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多