查找 Jersey Parent 路径
Find Jersey Parent Path
我有一个 Jersey 2.x 端点,用户可以 POST 到它来更新 parent 资源上的特定 属性。成功后,我想 return 303 状态并在 Location
header.
中指定 parent 资源的路径
例如。如果用户 POSTed 到:
http://example.com/api/v1/resource/field
然后在响应中设置位置header:
http://example.com/api/v1/resource
似乎应该有一种直接的方法可以用 UriInfo
/UriBuilder
来做到这一点,但我不知道如何在没有 hard-coding 的情况下做到这一点以后可能会坏掉。
从 UriInfo.getBaseUriBuilder()
, then append the XxxResource
path with builder.path(XxxResource.class)
.
获取基础 URI(假设是 http://example.com/api
)
然后自建URI
、returnResponse.seeOther(uri).build();
。完整示例:
@Path("/v1/resource")
public class Resource {
@GET
public Response getResource() {
return Response.ok("Hello Redirects!").build();
}
@POST
@Path("/field")
public Response getResource(@Context UriInfo uriInfo) {
URI resourceBaseUri = uriInfo.getBaseUriBuilder()
.path(Resource.class)
.build();
return Response.seeOther(resourceBaseUri).build();
}
}
我有一个 Jersey 2.x 端点,用户可以 POST 到它来更新 parent 资源上的特定 属性。成功后,我想 return 303 状态并在 Location
header.
例如。如果用户 POSTed 到:
http://example.com/api/v1/resource/field
然后在响应中设置位置header:
http://example.com/api/v1/resource
似乎应该有一种直接的方法可以用 UriInfo
/UriBuilder
来做到这一点,但我不知道如何在没有 hard-coding 的情况下做到这一点以后可能会坏掉。
从 UriInfo.getBaseUriBuilder()
, then append the XxxResource
path with builder.path(XxxResource.class)
.
http://example.com/api
)
然后自建URI
、returnResponse.seeOther(uri).build();
。完整示例:
@Path("/v1/resource")
public class Resource {
@GET
public Response getResource() {
return Response.ok("Hello Redirects!").build();
}
@POST
@Path("/field")
public Response getResource(@Context UriInfo uriInfo) {
URI resourceBaseUri = uriInfo.getBaseUriBuilder()
.path(Resource.class)
.build();
return Response.seeOther(resourceBaseUri).build();
}
}