本文介绍了Gradle脚本要在没有任何第三方插件的情况下调用REST Web服务,任何指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要在没有任何第三方插件的情况下调用REST服务作为buildscript(Gradle)的一部分,我如何使用Groovy来做到这一点?

(我的第一次尝试)

 存储库{
mavenCentral()
}
依赖关系{
complieorg.codehaus.groovy.modules.http-builder:http-builder:0.5.2
}

任务hello {
def http = new HTTPBuilder( http://myserver.com:8983/solr/select?q=*&wt=json)
http.auth.basic'用户名','密码'
http.request(GET ,JSON){req - >
}
}


解决方案

在不使用外部库的情况下从groovy调用REST的最简单方法是执行CURL。下面是一个调用Artifactory的例子,返回JSON并解析它:

  import groovy.json.JsonSlurper 

任务hello {
def p = ['curl','-u',''admin:password'',\http:// localhost:8081 / api / storage / libs-release-本地?list& deep = 1 \] .exe()
def json = new JsonSlurper().parseText(p.text))
}


Hi I need to call a REST service as part of the buildscript (Gradle) without any 3rd party plugins, how could I use Groovy to do that?

(My first attempt)

repositories {
    mavenCentral()
}
      dependencies {  
            complie "org.codehaus.groovy.modules.http-builder:http-builder:0.5.2"  
    }  

task hello {
    def http = new HTTPBuilder("http://myserver.com:8983/solr/select?q=*&wt=json")
    http.auth.basic 'username', 'password'
    http.request(GET, JSON ) { req ->
    }
}
解决方案

The easiest way to call REST from groovy without external libraries is executing CURL. Here's an example of calling Artifactory, getting JSON back and parsing it:

import groovy.json.JsonSlurper

task hello {
    def p = ['curl', '-u', '"admin:password"', "\"http://localhost:8081/api/storage/libs-release-local?list&deep=1\""].execute()
    def json = new JsonSlurper().parseText(p.text))
}

这篇关于Gradle脚本要在没有任何第三方插件的情况下调用REST Web服务,任何指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 17:58