【问题标题】:Groovy script to Grails app到 Grails 应用程序的 Groovy 脚本
【发布时间】:2013-08-28 09:29:19
【问题描述】:

我是 Groovy/Grails 的新手。我编写了一个 Groovy 脚本,它使用 RESTClient 向 JIRA 服务器发出 HTTP POST 请求。 POST 请求发送 JQL 查询并以 JSON 格式接收结果。完整代码如下:

import groovyx.net.http.RESTClient;
import groovyx.net.http.HttpResponseDecorator;
import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;
import groovy.json.JsonSlurper;
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*

@Grab(value = 'org.codehaus.groovy:groovy-all:2.1.6', 
      initClass = false)
@Grapes([
@Grab(group = 'org.codehaus.groovy.modules.http-builder', 
      module = 'http-builder', version = '0.5.2'),
@GrabExclude('org.codehaus.groovy:groovy')
])

// connect to JIRA
def jiraApiUrl = 'http://my-jira.com/rest/api/2/'
def jiraClient = new RESTClient(jiraApiUrl);

// authentication
def basic = 'Basic ' + 'username:password'.bytes.encodeBase64().toString()
jiraClient.client.addRequestInterceptor (
new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, 
                 HttpContext httpContext) {
                      httpRequest.addHeader('Authorization', basic)
      }
    })

// http post method
def uriPath = 'search'
def param = [maxResults : 1, jql : '<jql-query>']

def Issues = jiraClient.post(requestContentType : JSON, path : uriPath, body : param)

def slurpedIssues = new JsonSlurper().parseText(Issues.data.toString())

println Issues.data.total

我需要将此脚本迁移到 Grails 应用程序。关于如何做同样的任何建议?

【问题讨论】:

    标签: grails groovy


    【解决方案1】:
    1. 在 BuildConfig 中定义依赖项(groovy 依赖项除外)
    2. 将脚本内容复制到服务中

    可能的扩展:

    1. 使用grails rest plugingrails rest-client-builder plugin 代替http-builder

    【讨论】:

    • 感谢您的回复!非常感激。我添加了依赖项并将脚本复制到 Controller。当我从 Web 浏览器访问控制器时,它给了我一个 NullPointerException。 “消息:无法在空对象上调用方法 post()” 在服务中也发生了同样的情况。有什么建议吗?
    • 听起来jiraClient 为空。值得检查您是否正确初始化它。
    • 感谢您的提示!初始化有问题。但是现在得到一个新的 NullPointerException。叹!同一行,def Issues = jiraClient.post(requestContentType : JSON, path : uriPath, body : param)。 “消息:空”。
    • JSON 一个类,还是应该是一个字符串(即'JSON')?
    • 很少有错误,已纠正。谢谢!我无法编辑contentType,这给了我一个Unsupported Media Type 异常。
    【解决方案2】:

    将逻辑放入 Service 对象将使您能够进行依赖注入,这是 Grails 服务的本机。

    此外,如果您的应用有很多用户试图发出请求,您应该考虑使用AsyncHTTPBuilder

    【讨论】:

      【解决方案3】:

      我坚信服务响应会直接呈现为 JSON

        //your controller 
          class AbcController{
      
          //your action   
          def save() {
          render(abcService.save(params) as JSON)//your service response now been rendered to JSON
              }
      
          }
      
      //your service class    class AbcService {
          def save(params){
           ....
           return something
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多