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
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01