Java 在 Jersey 测试调用上设置查询参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24770027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Query Parameters on a Jersey Test Call
提问by kwiqsilver
I have a Jersey based Java servlet:
我有一个基于 Jersey 的 Java servlet:
@Path("foo")
class Foo {
@GET
@Path("bar")
public Response bar(@QueryParam("key") String value) {
// ...
}
}
I can call it in Tomcat just fine as:
我可以在 Tomcat 中调用它就好了:
http://localhost:8080/container/foo/bar?key=blah
However, in my JerseyTest, using Grizzly, it's not handling the parameters properly. This test case returns a 404 error:
然而,在我的 JerseyTest 中,使用 Grizzly,它没有正确处理参数。此测试用例返回 404 错误:
@Test
public void testBar() {
final Response response = target("foo/bar?key=blah").request().get();
}
I suspect the issue is it's looking for a resource named foo/bar?key=blah
rather than trying to pass key=blah
to the resource at foo/bar
. If I pass just "foo/bar"
to target()
, I get a 500, as the code throws an exception for a null parameter.
我怀疑问题在于它正在寻找一个命名的资源,foo/bar?key=blah
而不是试图传递key=blah
给foo/bar
. 如果我只传递"foo/bar"
到target()
,我会得到 500,因为代码会为空参数引发异常。
I looked through the Jersey Test documentation, and some examples, and I found some cryptic looking stuff that might have been for passing parameters to a GET, but none of it looked like it was assigning values to parameters, so I wasn't positive how I would use it.
我查看了 Jersey 测试文档和一些示例,我发现了一些看起来很神秘的东西,它们可能用于将参数传递给 GET,但没有一个看起来像是在为参数赋值,所以我不确定如何我会使用它。
How can I pass my value in for that parameter?
如何为该参数传递我的值?
采纳答案by Michal Gajdos
JavaDoc to WebTarget.queryParam()should give you an answer to your problem. Basically you need to transform your code to something like:
JavaDoc 到WebTarget.queryParam()应该给你一个你的问题的答案。基本上,您需要将代码转换为以下内容:
target("foo/bar").queryParam("key", "blah").request().get()