Jersey - Redirect after POST to outside URL(泽西岛 - 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();
}
这篇关于泽西岛 - POST 后重定向到外部 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:泽西岛 - POST 后重定向到外部 URL
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01