Can anyone suggest me which is the best approach to redirect an URL using REST among the below two ways:
1. httpResponse.sendRedirect("URL");
2. Response.temporaryRedirect(new URI("path"));
Can anyone suggest me which is the best approach to redirect an URL using REST among the below two ways:
1. httpResponse.sendRedirect("URL");
2. Response.temporaryRedirect(new URI("path"));
There are many forms of redirect. The 3xx
family of HTTP status codes contains:
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?
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
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.
The correct one depend on your needs. However, these are the most commons:
6.4.2. 301 Moved Permanently
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. [...]
6.4.4. 303 See Other
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. [...]
6.4.7. 307 Temporary Redirect
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. [...]
By the code you posted in the question, I believe you are using the JAX-RS API. If so, you can perform the redirects as following:
URI uri = ...
return Response.status(Status.MOVED_PERMANENTLY).location(uri).build();
URI uri = ...
return Response.seeOther(uri).build();
URI uri = ...
return Response.temporaryRedirect(uri).build();
For more details, the Response
class documentation may be useful.
You also can inject the UriInfo
in your REST endpoints:
@Context
UriInfo uriInfo;
And get some useful information, such as the base URI and the absolute path of the request. It will be useful when building the URI for redirection.
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();
}
}