我如何从Grails控制器中使用Restful API?

时间:2019-07-08 11:48:36

标签: java api grails

我已经从一家移动公司购买了批量短信。他们向我提供了api文档。我可以通过ajax调用使用api发送消息。但是出于安全性考虑,我想从服务器端而不是客户端使用api。

我已经尝试过grails.plugins.rest.client.RestBuilder,但是没有获得适当的文档。

在我使用的插件依赖项中:编译“ org.grails.plugins:rest-client-builder:2.1.1”

我想从我的grails控制器中使用该API

1 个答案:

答案 0 :(得分:0)

这个问题似乎很广泛。有很多方法可以从任何JVM应用程序(包括Grails应用程序)进行调用。

一种常见的方法是使用RestBuilder中的org.grails:grails-datastore-rest-client

def rest = new RestBuilder()

// generate a GET request
def response = rest.get('https://someurl.com/someuri')
def json = response.json
def firstName = json.firstName
def lastName = json.lastName
// etc...

// generate a POST request
response = rest.post('https://someurl.com/someuri') {
    // this results in the body of the request being
    // {"firstName":"Jeff","lastName":"Brown"} and
    // the content type being set to application/json
    json {
        firstName = 'Jeff'
        lastName = 'Brown'
    }
}
相关问题