Java 使用 REST 重定向 URL 的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36638150/
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
Best approach to redirect an URL using REST
提问by user1770589
Can anyone suggest me which is the best approach to redirect an URL using REST among the below two ways:
任何人都可以建议我在以下两种方式中使用 REST 重定向 URL 的最佳方法是什么:
1. httpResponse.sendRedirect("URL");
2. Response.temporaryRedirect(new URI("path"));
采纳答案by user1770589
There are many forms of redirect. The 3xx
family of HTTP status codes contains:
重定向的形式有很多种。该3xx
HTTP状态代码系列包含:
301 Moved Permanently
307 Temporary Redirect
301 Moved Permanently
307 Temporary Redirect
These and the other codes have different semantic. Which is right depends on your situation. Have the resources been permanently been moved to a new location? Or is the redirect only temporary?
这些和其他代码具有不同的语义。哪个是正确的取决于你的情况。资源是否已永久移动到新位置?或者重定向只是暂时的?
回答by cassiomolin
There are several types of redirects
有几种类型的重定向
According to the RFC 7231, the current reference for the semantics and content of the HTTP/1.1, there are several types of redirects. They are all
根据RFC 7231(HTTP/1.1 语义和内容的当前参考),重定向有多种类型。他们都是
Redirects that indicate the resource might be available at a different URI, as provided by the Location field, as in the status codes
301
(Moved Permanently),302
(Found), and307
(Temporary Redirect).Redirection that offers a choice of matching resources, each capable of representing the original request target, as in the
300
(Multiple Choices) status code.Redirection to a different resource, identified by the Location field, that can represent an indirect response to the request, as in the
303
(See Other) status code.Redirection to a previously cached result, as in the
304
(Not Modified) status code.
指示资源可能在不同 URI 处可用的重定向,由 Location 字段提供,如状态代码
301
(Moved Permanently)、302
(Found) 和307
(Temporary Redirect)。提供匹配资源选择的重定向,每个资源都能够表示原始请求目标,如
300
(Multiple Choices) 状态代码。重定向到不同的资源,由 Location 字段标识,可以表示对请求的间接响应,如
303
(见其他)状态代码。重定向到先前缓存的结果,如
304
(未修改)状态代码中所示。
The correct one depend on your needs. However, these are the most commons:
正确的选择取决于您的需求。但是,这些是最常见的:
The
301
(Moved Permanently) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. [...]
该
301
(永久移动)状态代码表示目标资源已经被分配了一个新的永久性URI,该资源的任何将来参考应该使用封闭的URI之一。[...]
The
303
(See Other) status code indicates that the server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, which is intended to provide an indirect response to the original request. [...]
的
303
(见其他)状态代码表示该服务器将用户重定向代理不同的资源,如通过一URI在Location头字段,其旨在提供对原始请求的间接响应中指示。[...]
The
307
(Temporary Redirect) status code indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. [...]
的
307
(临时重定向)状态代码表示它是否执行自动重定向到该URI暂时下一个不同的URI和用户代理目标资源驻留不能改变的请求方法。[...]
Performing the redirects in JAX-RS
在 JAX-RS 中执行重定向
By the code you posted in the question, I believe you are using the JAX-RSAPI. If so, you can perform the redirects as following:
根据您在问题中发布的代码,我相信您正在使用JAX-RSAPI。如果是这样,您可以执行以下重定向:
URI uri = ...
return Response.status(Status.MOVED_PERMANENTLY).location(uri).build();
- 303 See Other(aka POST/redirect/GET):
- 303 见其他(又名 POST/redirect/GET):
URI uri = ...
return Response.seeOther(uri).build();
URI uri = ...
return Response.temporaryRedirect(uri).build();
For more details, the Response
class documentation may be useful.
有关更多详细信息,Response
类文档可能有用。
Other details that can be useful when using JAX-RS
使用 JAX-RS 时可能有用的其他详细信息
You also can inject the UriInfo
in your REST endpoints:
您还可以UriInfo
在 REST 端点中注入:
@Context
UriInfo uriInfo;
And get some useful information, such as the base URIand the absolute path of the request. It will be useful when building the URI for redirection.
并获取一些有用的信息,例如基本 URI和请求的绝对路径。在构建用于重定向的 URI 时,它会很有用。
A resource method with redirection will be like:
具有重定向的资源方法将如下所示:
@Path("/foo")
public class MyEndpoint {
@Context
private UriInfo uriInfo;
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
URI uri = uriInfo.getBaseUriBuilder().path("bar").build();
return Response.temporaryRedirect(uri).build();
}
}