37

Is it possible to have a JAX-RS web service redirect to another web page?

Like as you would do with Servlet response.sendRedirect("http://test/test.html").

The JAX-RS web service should itself redirect. I'm using RESTEasy if that's relevant.

4
  • 1
    The question is not clear, do you mean that the rest response should itself redirect, or that it should return a url that the caller can use as a destination url? Feb 24, 2012 at 17:46
  • can you elaborate more on your question?? It's not clear...
    – Daniele B
    Feb 24, 2012 at 17:46
  • @BenParsons - the REST web service should itself redirect.
    – c12
    Feb 24, 2012 at 19:54
  • Here's a complete example: stackoverflow.com/a/13265693/712526
    – jpaugh
    Mar 2, 2016 at 16:16

3 Answers 3

57

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.

4
  • 3
    Then it's done the same way because they both use javax.ws.rs.core.Response.
    – smcg
    Feb 24, 2012 at 23:16
  • This link is broken Jul 2, 2017 at 2:53
  • 2
    What if I want to return a permanent redirect (301) ? Should I use temporaryRedirect or seeOther or another method ?
    – ihebiheb
    Jun 14, 2019 at 13:59
  • 1
    And what about for 302?
    – ndtreviv
    Aug 15, 2019 at 12:57
5

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

2

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>"))
    }
}
1
  • I haven't downvoted your reply. Please can you provide your example in Java rather than in Kotlin?
    – gouessej
    Sep 21, 2022 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.