Groovy servlet 在 Tomcat 中找不到 JAR 文件
我创建了一个 groovlet,它将充当一种 HTTP 代理。它接受 GET 请求,然后根据请求中提供的 URL 进行 Web 服务调用。
这是我到目前为止得到的代码:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
String url = params.url
def http = new HTTPBuilder(url)
http.request(GET, TEXT) {
response.success = { resp, reader ->
println reader
}
response.'404' = { resp ->
println 'not found!'
}
}
我得到了 Groovy HTTPBuilder JAR 文件位于 groovlet 的 WEB-INF/lib 文件夹中。但是,该代码没有按预期工作。 (我还尝试将文件夹放入 $TOMCAT_HOME/common/lib
中,结果与下面相同。)
当我完全按照上面的方式运行代码时,页面完全返回空白。
如果我只删除顶部的 @Grab
行(因为 JAR 理论上应该已经在类路径中),我会从 Tomcat 收到一个令人讨厌的错误(我在 5.5 上运行它,但我得到6) 上的行为大致相同:
HTTP 状态 500 - GroovyServlet 错误: 脚本:'/proxy.groovy':脚本 处理失败。启动失败: 语义期间的一般错误 分析:类型 org.apache.http.client.methods.HttpRequestBase 不存在 java.lang.TypeNotPresentException: 类型 org.apache.http.client.methods.HttpRequestBase 不存在...
然后是堆栈跟踪。
我的groovlet 有什么问题吗?
I've created a groovlet that will act as a sort of HTTP proxy. It accepts GET requests and then makes web service calls based on the URL provided in the request.
Here's the code I've got so far:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
String url = params.url
def http = new HTTPBuilder(url)
http.request(GET, TEXT) {
response.success = { resp, reader ->
println reader
}
response.'404' = { resp ->
println 'not found!'
}
}
I've got the Groovy HTTPBuilder JAR file in the WEB-INF/lib
folder of the groovlet. However, the code isn't working as it should. (I also tried putting the folder in $TOMCAT_HOME/common/lib
with the same results as below.)
When I run the code exactly as it is above, the page comes back completely blank.
If I remove just the @Grab
line at the top (since the JAR should theoretically be in the classpath already), I get a nasty error from Tomcat (I'm running it on 5.5, but I get roughly the same behavior on 6):
HTTP Status 500 - GroovyServlet Error:
script: '/proxy.groovy': Script
processing failed.startup failed:
General error during semantic
analysis: Type
org.apache.http.client.methods.HttpRequestBase
not present
java.lang.TypeNotPresentException:
Type
org.apache.http.client.methods.HttpRequestBase
not present...
That is then followed by the stack trace.
What is wrong with my groovlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事。
首先,Groovlets 似乎无法使用 Grape(
@Grab
命令)。这就是当这条线存在时 groovlet 会默默地失败的原因。其次,
http-builder
模块还依赖于大约 19 个其他包(包括错误消息中引用的org.apache.http.client.methods.HttpRequestBase
) 。您可以在~/.groovy/grapes
文件夹中找到这些包。如果要找到所有依赖项,请删除 Grapes 目录。然后在本地运行使用该
@Grab
命令的脚本。脚本执行完毕后,您可以查看 Grapes 目录并查看它下载的所有内容。当我这样做时,我得到了以下包列表:
因此,如果您想在 groovlet 中使用 HttpBuilder,您需要在 WEB-INF/lib 或 Tomcat common/lib 目录中获取所有这些依赖项。
另一方面,如果您不需要任何非常奇特的东西,则可以使用 Groovy URL 对象。请参阅此处的一些示例。
Two things.
First, it seems that Groovlets can't use Grape (the
@Grab
command). That's why the groovlet fails silently when this line is present.Second, the
http-builder
module also depends on about 19 other packages (including theorg.apache.http.client.methods.HttpRequestBase
that is referenced in the error message). You can find these packages in the~/.groovy/grapes
folder.If you want to find all the dependencies, delete the Grapes directory. Then run a script locally that uses that
@Grab
command. Once the script has finished executing, you can look in the Grapes directory and see everything it downloaded.When I did it, I got the following list of packages:
So if you want to use HttpBuilder in a groovlet, you'll need to get all those dependencies in WEB-INF/lib or your Tomcat common/lib directory.
On the other hand, if you don't need anything terribly fancy, you can use the Groovy URL object. See some examples here.