如何将多个查询参数映射到 Jersey GET 请求上的 bean 字段?
服务类具有接受多个参数的@GET
操作。这些参数作为查询参数传递到 @GET
服务调用。
@GET
@Path("find")
@Produces(MediaType.APPLICATION_XML)
public FindResponse find(@QueryParam("prop1") String prop1,
@QueryParam("prop2") String prop2,
@QueryParam("prop3") String prop3,
@QueryParam("prop4") String prop4, ...)
这些参数的列表正在不断增长,因此我想将它们放入包含所有这些参数的单个 bean 中。
@GET
@Path("find")
@Produces(MediaType.APPLICATION_XML)
public FindResponse find(ParameterBean paramBean)
{
String prop1 = paramBean.getProp1();
String prop2 = paramBean.getProp2();
String prop3 = paramBean.getProp3();
String prop4 = paramBean.getProp4();
}
你会怎么做?这可能吗?
A service class has a @GET
operation that accepts multiple parameters. These parameters are passed in as query parameters to the @GET
service call.
@GET
@Path("find")
@Produces(MediaType.APPLICATION_XML)
public FindResponse find(@QueryParam("prop1") String prop1,
@QueryParam("prop2") String prop2,
@QueryParam("prop3") String prop3,
@QueryParam("prop4") String prop4, ...)
The list of these parameters are growing, so I would like to place them into a single bean that contains all these parameters.
@GET
@Path("find")
@Produces(MediaType.APPLICATION_XML)
public FindResponse find(ParameterBean paramBean)
{
String prop1 = paramBean.getProp1();
String prop2 = paramBean.getProp2();
String prop3 = paramBean.getProp3();
String prop4 = paramBean.getProp4();
}
How would you do this? Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Jersey 2.0 中,您需要使用 BeanParam 可无缝提供您在正常 Jersey 风格中寻找的内容。
从上面链接的文档页面中,您可以使用 BeanParam 执行以下操作:
然后 ParameterBean.java 将包含:
我更喜欢参数 bean 上的公共属性,但您可以使用 getters/setters 和私有字段如果你也喜欢的话。
In Jersey 2.0, you'll want to use BeanParam to seamlessly provide what you're looking for in the normal Jersey style.
From the above linked doc page, you can use BeanParam to do something like:
And then
ParameterBean.java
would contain:I prefer public properties on my parameter beans, but you can use getters/setters and private fields if you like, too.
尝试这样的事情。使用 UriInfo 将所有请求参数放入映射中并尝试访问它们。这是代替传递单个参数来完成的。
Try something like this. Use UriInfo to get all the request parameters into a map and try to access them. This is done inplace of passing individual parameters.
您可以使用
com.sun.jersey.spi.inject.InjectableProvider
。在您的资源中,您只需使用
@QueryParam("valueWeDontCare")
。将自动调用提供者。
You can use
com.sun.jersey.spi.inject.InjectableProvider
.In your resource you just have to use
@QueryParam("valueWeDontCare")
.The provider will be automatically called.
您可以创建自定义提供程序。
You can create a custom Provider.
您可能想要使用以下方法。这是一个非常符合标准的解决方案,并且没有任何黑客行为。上面的解决方案也有效,但有点 hacky,因为它表明它只处理请求正文,而它从上下文中提取数据。
就我而言,我想创建一个注释,允许将查询参数“限制”和“偏移”映射到单个对象。解决办法如下:
你还需要做的就是注册它。
您还需要注释本身
和一个 bean
然后您可以像这样使用它
You might want to use the following approach. This is a very standard-compliant solution and there are no hacks in there. The above solution also works but is somewhat hacky because it suggests it deals only with request body whereas it extracts the data from the context instead.
In my case I wanted to create an annotation which would allow to map query parameters "limit" and "offset" to a single object. The solution is as follows:
What you also need to do is registering it.
You also need the annotation itself
and a bean
Then you might use it like this
我知道我的回答并不适用于具体情况。但由于 WEB 传输机制无论如何都应该与核心应用程序分离,因此可以选择更改为其他 Web 框架。就像 Spring webmvc 开箱即用地完成所有这些工作。
I know that my answer is not applicable to the specific context. But as the WEB transport mechanism should be separated from the the core application anyway it could be the option to change to other web framework. Like Spring webmvc is doing all of this out of the box.