泽西-后重定向到外部URL

时间:2022-10-20 09:35:52

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.

我使用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.

我在这里查看了关于这个主题的其他类似问题,但是没有找到任何我可以使用的东西。

1 个解决方案

#1


25  

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.

我建议修改jax - rs注释方法的签名,以返回一个javax.ws.r .core。响应对象。取决于您希望重定向是永久的还是临时的(例如,客户端是否应该更新其内部引用以反映新地址),该方法应该构建并返回与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();
}

#1


25  

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.

我建议修改jax - rs注释方法的签名,以返回一个javax.ws.r .core。响应对象。取决于您希望重定向是永久的还是临时的(例如,客户端是否应该更新其内部引用以反映新地址),该方法应该构建并返回与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();
}