首页 > 解决方案 > 从微服务中获取数据以存放在 grails 中

问题描述

我有一个休息和一个微服务。在微服务中,我有一个表格,我希望将该表格数据提取到休息,我在休息演示控制器中编写了以下方式。

def result = restBuilder().post("http://localhost:2222/api/microservice/fetchData"){
            header 'authorization', 'fdgtertddfgfdgfffffff'
            accept("application/json")
            contentType("application/json")
            json "{'empId':1,'ename':'test1'}"
        }

但它会引发错误“没有方法签名:demoController.restBuilder() 适用于参数类型:() 值:[]”。我应该如何从微服务中获取数据以进行休息?

标签: grailsmicroservices

解决方案


您正在调用一个名为的方法restBuilder(),但该方法不存在。如果您希望它起作用,您将需要实现该方法并让它返回可以处理对post(String, Closure).

您可能打算使用RestBuilder该类。具体细节取决于您使用的 Grails 版本,但您可能想要的是这样的......

RestBuilder restBuilder = new RestBuilder()
restBuilder.post('http://localhost:2222/api/microservice/fetchData'){
    header 'authorization', 'fdgtertddfgfdgfffffff'
    accept 'application/json'
    json {
        empId = 1
        name = 'test1'
    }
}

您可能需要grails-datastore-rest-clientbuild.gradle.

compile "org.grails:grails-datastore-rest-client"

我希望这会有所帮助。


推荐阅读