尝试使用Groovy HTTPBuilder会为groovy/util/slurpersupport/GPathResult抛出NoClassDefFoundError

1wnzp6jl  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(119)

我正在学习UDEMYThe Complete Apache Groovy Developer Course第112节。使用基于REST的API,有一些提供的示例代码不起作用。示例代码为:

import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')

String base = 'http://api.icndb.com'

def chuck = new RESTClient(base)
def params = [firstName: "Dan", lastName: "Vega"]

chuck.contentType = ContentType.JSON
chuck.get( path: '/jokes/random', query: params ) { response, json ->
    println response.status
    println json
}

且误差

groovy chuck.groovy
Caught: java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult
java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult

**问题:尝试使用HttpClient显然会失败,因为代码使用了Groovy中不存在的东西(或者这是我对这个问题的看法)。

问题可能是由于**旧的 HttpBuilder 代码试图使用新的Groovy版本中不再存在的东西。(这是我的怀疑)。
我使用的Java和Groovy版本是:

groovy --version
Groovy Version: 4.0.0 JVM: 17.0.2 Vendor: Oracle Corporation OS: Windows 10

我尝试搜索问题的答案,发现https://stackoverflow.com/a/34814548/3281336提供了一种验证HttpBuilder是否找到的方法,但它也失败了,出现了同样的错误。
在HttpBuilder上显示该软件在软件年中相当旧(2014年左右是最后一次更新,请参阅Maven Repository for HttpBuilder)。
HttpBuilder已移动,请参阅Where does the groovy http-builder module live now?
即使是在https://github.com/http-builder-ng/http-builder-ng上的一个较新的HttpBuilder分支也显示了2020年5月的最后一次提交。接下来我将尝试该代码,但需要学习一种新的方法来导入代码以使用它。
我现在要回到课堂上,跳过这个模块,当问到这个问题时,我会指向这个链接。也许老师会回答。:-)

9udxz4iz

9udxz4iz1#

HTTPBuilder不支持groovy 4。一些组件在从codehaus迁移到apache的过程中移动了包。它可以在groovy 3上运行,只需要做一些小的修改。它与Groovy 2一起工作得很好。

相关问题