Java Jersey - POST 后重定向到外部 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25330700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:06:50  来源:igfitidea点击:

Jersey - Redirect after POST to outside URL

javarestjersey

提问by Vuk Stankovi?

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.comthat doesn't have to be related to API.

我正在使用 Jersey 创建 REST API。我有一个 POST 方法,作为该方法的响应,用户应该被重定向到自定义 URL,这样http://example.com就不必与 API 相关。

I was looking at other similar questions on this topic here, but didn't find anything that I could use.

我在这里查看了有关此主题的其他类似问题,但没有找到任何可以使用的内容。

采纳答案by sumitsu

I'd suggest altering the signature of the JAX-RS-annotated method to return a javax.ws.rs.core.Responseobject. 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 Responsecorresponding to an HTTP-301 (permanent redirect)or HTTP-302 (temporary redirect)status code.

我建议更改 JAX-RS 注释方法的签名以返回一个javax.ws.rs.core.Response对象。根据您希望重定向是永久的还是临时的(即客户端是否应更新其内部引用以反映新地址),该方法应构建并返回Response对应于HTTP-301(永久重定向)HTTP -302(临时重定向)状态代码。

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:

以下是 Jersey 文档中关于如何返回自定义 HTTP 响应的描述:https: //jersey.java.net/documentation/latest/representations.html#d0e5151。我还没有测试过以下代码段,但我想对于 HTTP-301,代码看起来像这样:

@POST
public Response yourAPIMethod() {
    URI targetURIForRedirection = ...;
    return Response.seeOther(targetURIForRedirection).build();
}

...or this, for HTTP-302:

...或者这个,对于 HTTP-302:

@POST
public Response yourAPIMethod() {
    URI targetURIForRedirection = ...;
    return Response.temporaryRedirect(targetURIForRedirection).build();
}