如何获取 Jersey JaxRS 中的所有查询参数?

发布于 2024-11-02 04:28:44 字数 56 浏览 8 评论 0原文

我正在构建一个通用的 Web 服务,需要将所有查询参数抓取到一个字符串中以供稍后解析。我该怎么做?

I am building a generic web service and need to grab all the query parameters into one string for later parsing. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2024-11-09 04:28:44

您可以通过 @QueryParam("name") 访问单个参数,也可以通过上下文访问所有参数:

@POST
public Response postSomething(@QueryParam("name") String name, @Context UriInfo uriInfo, String content) {
     MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 
     String nameParam = queryParams.getFirst("name");
}

关键是 @Context jax-rs注释,可用于访问:

UriInfo、请求、HttpHeaders、
SecurityContext,提供者

You can access a single param via @QueryParam("name") or all of the params via the context:

@POST
public Response postSomething(@QueryParam("name") String name, @Context UriInfo uriInfo, String content) {
     MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 
     String nameParam = queryParams.getFirst("name");
}

The key is the @Context jax-rs annotation, which can be used to access:

UriInfo, Request, HttpHeaders,
SecurityContext, Providers

错爱 2024-11-09 04:28:44

请求 URI 的未解析查询部分可以从 UriInfo 对象中获取:

@GET
public Representation get(@Context UriInfo uriInfo) {
  String query = uriInfo.getRequestUri().getQuery();
  ...
}

The unparsed query part of the request URI can be obtained from the UriInfo object:

@GET
public Representation get(@Context UriInfo uriInfo) {
  String query = uriInfo.getRequestUri().getQuery();
  ...
}
叹沉浮 2024-11-09 04:28:44

在已接受的答案中添加更多内容。还可以通过以下方式获取所有查询参数,而无需向方法添加额外的参数,这在维护 swagger 文档时可能很有用。

@Context
private UriInfo uriInfo;

@POST
public Response postSomething(@QueryParam("name") String name) {
     MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 
     String nameParam = queryParams.getFirst("name");
}

参考

Adding a bit more to the accepted answer. It is also possible to get all the query parameters in the following way without adding an additional parameter to the method which maybe useful when maintaining swagger documentation.

@Context
private UriInfo uriInfo;

@POST
public Response postSomething(@QueryParam("name") String name) {
     MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 
     String nameParam = queryParams.getFirst("name");
}

ref

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文