如何在 JAX-RS 中获取当前调度的 Web 资源的 URI?
如何获取当前在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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 aUriBuilder
from one of itsget*Builder
methods.