[英]Missing classes of HttpBuilder in Groovy
Here is simple request I try to do and watch response这是我尝试执行的简单请求并观察响应
def httpReq = new HTTPBuilder("http://${url}")
httpReq.request(Method.GET, ContentType.JSON) {
response.success = { resp ->
println("Ok " + resp.status.toString())
}
response.failure = { resp ->
println("Not ok" + resp.status.toString())
}
This returns an Error:这将返回一个错误:
java.lang.ClassNotFoundException: org.apache.commons.collections.iterators.ArrayIterator
java.lang.ClassNotFoundException: org.apache.commons.collections.iterators.ArrayIterator
I think that ArrayIterator defined in jdk.我认为 ArrayIterator 定义在 jdk 中。 I added manually
org.apache.commons:commons-collections4:4.4
but it doesn't help.我手动添加了
org.apache.commons:commons-collections4:4.4
但它没有帮助。
Don't waste your time by gathering deps.不要通过收集 deps 来浪费你的时间。 Use a build tool like Gradle or Maven - or use Groovys Grapes if only a script is needed and Groovy is installed (or easily installable) on your target.
使用 Gradle 或 Maven 之类的构建工具 - 如果只需要脚本并且 Groovy 已安装(或易于安装)在您的目标上,则使用 Groovys Grapes。
grape
grape
You have to pick up all the transitive dependencies (this means the correct packages with the correct versions).您必须选择所有可传递的依赖项(这意味着具有正确版本的正确包)。 Eg with
@Grab
:例如与
@Grab
:
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovyx.net.http.HTTPBuilder
def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )
If you really want to gather all the deps manually grape
can also help.如果你真的想手动收集所有的 deps,
grape
也可以提供帮助。 It can tell you, what jars are needed:它可以告诉你,需要什么罐子:
% grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.1
$HOME/.groovy/grapes/org.codehaus.groovy.modules.http-builder/http-builder/jars/http-builder-0.7.1.jar
$HOME/.groovy/grapes/org.apache.httpcomponents/httpclient/jars/httpclient-4.2.1.jar
$HOME/.groovy/grapes/org.apache.httpcomponents/httpcore/jars/httpcore-4.2.1.jar
$HOME/.groovy/grapes/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar
$HOME/.groovy/grapes/commons-codec/commons-codec/jars/commons-codec-1.6.jar
$HOME/.groovy/grapes/net.sf.json-lib/json-lib/jars/json-lib-2.3-jdk15.jar
$HOME/.groovy/grapes/commons-beanutils/commons-beanutils/jars/commons-beanutils-1.8.0.jar
$HOME/.groovy/grapes/commons-collections/commons-collections/jars/commons-collections-3.2.1.jar
$HOME/.groovy/grapes/commons-lang/commons-lang/jars/commons-lang-2.4.jar
$HOME/.groovy/grapes/net.sf.ezmorph/ezmorph/jars/ezmorph-1.0.6.jar
$HOME/.groovy/grapes/net.sourceforge.nekohtml/nekohtml/jars/nekohtml-1.9.16.jar
$HOME/.groovy/grapes/xerces/xercesImpl/jars/xercesImpl-2.9.1.jar
$HOME/.groovy/grapes/xml-apis/xml-apis/jars/xml-apis-1.3.04.jar
$HOME/.groovy/grapes/xml-resolver/xml-resolver/jars/xml-resolver-1.2.jar
From this list you can build up your own classpath to pass to groovy and work without grape.从这个列表中,您可以建立自己的类路径以传递给 groovy 并在没有葡萄的情况下工作。 Eg:
例如:
% cat no-grape.groovy
import groovyx.net.http.HTTPBuilder
def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )
% groovy -cp `grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.1 | paste -s -d:` no-grape.groovy
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.