Send redirect from a JAX-RS service

For those like me looking for 302 that fall on this answer.

By looking the code of

Response.temporaryRedirect(URI)

You can customize your response code like this :

Response.status(int).location(URI).build()

Note that status code are define in enum

Response.Status

And for example 302 is Response.Status.FOUND


Yes, you can do this in Jersey or any JAX-RS implementation (including RestEasy) if your return type is a Response (or HttpServletResponse) https://eclipse-ee4j.github.io/jersey.github.io/apidocs/1.19.1/jersey/javax/ws/rs/core/Response.html

You can use either of the following:

Response.temporaryRedirect(URI)

Response.seeOther(URI)

"Temporary Redirect" returns a 307 status code while "See Other" returns 303.


Extending smcg@ answer above,

You can achieve this by altering the request context in a ContainerRequestFilter by using ContainerRequestContext.setRequestUri(URI). If you see the JAX-RS specification (Section 6.2) here, there is a mention of @PreMatching request filters. According to the documentation;

A ContainerRequestFilter that is annotated with @PreMatching is executed upon receiving a client request but before a resource method is matched. Thus, this type of filter has the ability to modify the input to the matching algorithm (see Section 3.7.2) and, consequently, alter its outcome.

A very naive filter can be like this;

@PreMatching
class RedirectFilter: ContainerRequestFilter {
    override fun filter(requestContext: ContainerRequestContext?) {

      requestContext!!.setRequestUri(URI.create("<redirect_uri>"))
    }
}