泽西岛 - POST后重定向到外部URL [英] Jersey - Redirect after POST to outside URL
问题描述
我正在使用Jersey创建REST API。我有一个POST方法,作为该方法的响应,用户应该被重定向到自定义URL,如 http://example.com
,不必与之相关API。
I'm using Jersey to create REST API. I have one POST method and as a response from that method, user should be redirected to 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.
推荐答案
我建议更改JAX-RS注释方法的签名以返回 javax.ws.rs.core.Response
对象。根据您是希望重定向是永久性还是临时性(即客户端是否应更新其内部引用以反映新地址),该方法应构建并返回响应
对应于 HTTP-301(永久重定向)或 HTTP-302(临时重定向)状态代码。
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.
以下是Jersey文档中有关如何返回自定义HTTP响应的说明: https://jersey.java.net/documentation/latest/representations.html#d0e5151 。我没有测试过以下代码片段,但我认为代码看起来像这样,对于HTTP-301:
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();
}
...或者这个,对于HTTP-302:
...or this, for HTTP-302:
@POST
public Response yourAPIMethod() {
URI targetURIForRedirection = ...;
return Response.temporaryRedirect(targetURIForRedirection).build();
}
这篇关于泽西岛 - POST后重定向到外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!