泽西岛 - POST 后重定向到外部 URL
我正在使用 Jersey 创建 REST API.我有一个 POST 方法,作为该方法的响应,用户应该被重定向到不需要与 API 相关的自定义 URL,例如 http://example.com
.p>
我在此处查看有关此主题的其他类似问题,但没有找到任何我可以使用的东西.
解决方案我建议更改 JAX-RS 注释方法的签名以返回 javax.ws.rs.core.Response
对象.根据您希望重定向是永久的还是临时的(即客户端是否应该更新其内部引用以反映新地址),该方法应该构建并返回对应于 Response
a href="http://en.wikipedia.org/wiki/HTTP_301">HTTP-301(永久重定向) 或 HTTP-302(临时重定向) 状态码.
以下是 Jersey 文档中关于如何返回自定义 HTTP 响应的说明:https://jersey.java.net/documentation/latest/representations.html#d0e5151.我还没有测试以下代码段,但我想对于 HTTP-301,代码看起来像这样:
@POST公共响应 yourAPIMethod() {URI 目标URIForRedirection = ...;返回 Response.seeOther(targetURIForRedirection).build();}
...或者这个,对于 HTTP-302:
@POST公共响应 yourAPIMethod() {URI 目标URIForRedirection = ...;返回 Response.temporaryRedirect(targetURIForRedirection).build();}
I'm using Jersey to create REST API. I have one POST method and as a response from that method, the user should be redirected to a custom URL like http://example.com
that doesn't have to be related to API.
I was looking at other similar questions on this topic here but didn't find anything that I could use.
解决方案I'd suggest altering the signature of the JAX-RS-annotated method to return a javax.ws.rs.core.Response
object. Depending on whether you intend the redirection to be permanent or temporary (i.e. whether the client should update its internal references to reflect the new address or not), the method should build and return a Response
corresponding to an HTTP-301 (permanent redirect) or HTTP-302 (temporary redirect) status code.
Here's a description in the Jersey documentation regarding how to return custom HTTP responses: https://jersey.java.net/documentation/latest/representations.html#d0e5151. I haven't tested the following snippet, but I'd imagine that the code would look something like this, for HTTP-301:
@POST
public Response yourAPIMethod() {
URI targetURIForRedirection = ...;
return Response.seeOther(targetURIForRedirection).build();
}
...or this, for HTTP-302:
@POST
public Response yourAPIMethod() {
URI targetURIForRedirection = ...;
return Response.temporaryRedirect(targetURIForRedirection).build();
}
相关文章