如何在grails应用程序中使用API

问题描述 投票:9回答:2

我有一个正在此URL上运行的服务:http://localhost:8888

我通过像这样调用它来从该服务获得结果:

http://localhost:8888/colors?colorname=red&shade=dark

并且我将结果像这样以JSON格式返回:

 {
      "request#": 55,
      "colorname": "red",
      "shade": "dark",
      "available": "No"
 }

问题

我可以通过哪些方式在grails应用程序中使用此服务?

json rest grails
2个回答
14
投票

假设rest client builder的所有配置都在那里,您最终将得到2使用该服务的代码行如下:

//controller/service/POGO
def resp = rest.get("http://localhost:8888/colors?colorname=red&shade=dark")
resp.json //would give the response JSON

where

//resources.groovy
beans = {
    rest(grails.plugins.rest.client.RestBuilder)
}

0
投票

我曾经通过RestBuilder rest-client-builder引入了Grails 2.5.6应用程序中的plugin。我们不需要在RestBuilder中为resource.groovy类声明一个bean。

下面是我为my article演示的示例。

JSONElement retrieveBioModelsAllCuratedModels() {
    final String BM_SEARCH_URL = "https://wwwdev.ebi.ac.uk/biomodels/search?domain=biomodels"
    String queryURL = """\
${BM_SEARCH_URL}&query=*:* AND curationstatus:\"Manually curated\" AND NOT isprivate:true&format=json"""
    RestBuilder rest = new RestBuilder(connectTimeout: 10000, readTimeout: 100000, proxy: null)
    def response = rest.get(queryURL) {
        accept("application/json")
        contentType("application/json;charset=UTF-8")
    }
    if (response.status == 200) {
        return response.json
    }
    return null
}
© www.soinside.com 2019 - 2024. All rights reserved.