Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send redirect from a JAX-RS service

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.

like image 885
c12 Avatar asked Feb 24 '12 17:02

c12


People also ask

How do I redirect in Jax Runescape?

Because of the integration with JAX-RS it is possible to redirect to URLs the JAX-RS way. This means a redirect can be done by using a JAX-RS Response object as return value of the controller method which redirects. Behind the scenes it's a HTTP 303 "See Other". This is the first way to handle redirects.

How do I redirect a URL in Java?

The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.

What can a JAX-RS method return?

If the URI path template variable cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 400 (“Bad Request”) error to the client. If the @PathParam annotation cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 404 (“Not Found”) error to the client.

Is Jersey and JAX-RS same?

JAX-RS is an specification (just a definition) and Jersey is a JAX-RS implementation. Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides its own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development.


2 Answers

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.

like image 168
smcg Avatar answered Sep 20 '22 12:09

smcg


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

like image 21
user43968 Avatar answered Sep 19 '22 12:09

user43968