如何在 JAX-RS 中获取当前调度的 Web 资源的 URI?

发布于 2024-09-24 15:55:24 字数 376 浏览 2 评论 0原文

如何获取当前在 JAX-RS 中分派的资源的完整 URI?我正在尝试返回新创建的对象的 URI,并且需要它的前缀部分,包括主机、端口等:

// @import-s skipped
public class Factory {
  @POST
  public final Response create() {
    Integer id;
    // new object created and id is set
    return Response.created(
      URI.create(prefix + "/object/" + id)
    ).build();
  }
}

我在哪里可以获得这个 前缀 部分?

How can I get a full URI of the resource currently dispatched in JAX-RS? I'm trying to return a URI of newly created object, and need a prefix part of it, with host, port, etc.:

// @import-s skipped
public class Factory {
  @POST
  public final Response create() {
    Integer id;
    // new object created and id is set
    return Response.created(
      URI.create(prefix + "/object/" + id)
    ).build();
  }
}

Where can I get this prefix part?

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

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

发布评论

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

评论(1

一种方法是注入 UriInfo

public Final Response create(@Context UriInfo info) {...}

此时,您可以使用 info< /code> 直接或从其 get*Builder 方法之一获取 UriBuilder

One approach would be to inject UriInfo:

public final Response create(@Context UriInfo info) {...}

At that point, you can either use info directly or get a UriBuilder from one of its get*Builder methods.

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