Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard - how to do a server side redirect from a view?

I'm new to Drop Wizard, and would like to redirect from a server side view to another url in my app.

Does DropWizard wrap up this common task somehow?

e.g.

@GET
public View getView(@Context HttpServletRequest req)
{
     View view = new View();

     if (somethingBad)
     {
         // code here to redirect to another url, eg /bad_data
     }
     else
     {
          return view;
     }
}
like image 645
Brad Parks Avatar asked Dec 20 '13 17:12

Brad Parks


People also ask

What is Jersey in Dropwizard?

Jersey: Jersey is one of the best REST API implementations on the market. Also, it follows the standard JAX-RS specification, and it's the reference implementation for the JAX-RS specification. Dropwizard uses Jersey as the default framework for building RESTful web applications.

What is drop wizard?

What is Dropwizard? Dropwizard is an open source Java framework for developing ops-friendly, high-performance RESTful backends. It was developed by Yammer to power their JVM based backend. Dropwizard provides best-of-breed Java libraries into one embedded application package.


2 Answers

Here's a simple code example that actually does the redirect using a WebApplicationException. So you could put this in your view, or in your resource, and just throw it whenever.

URI uri2 = UriBuilder.fromUri(url).build();
Response response = Response.seeOther(uri2).build();
throw new WebApplicationException(response);

You can also just make your resource return either a view, or a redirect response:

@GET
public Object getView(@Context HttpServletRequest req)
{
     if (somethingBad())
     {
         URI uri = UriBuilder.fromUri("/somewhere_else").build();
         return Response.seeOther(uri).build();
     }

     return new View();
}
like image 60
Brad Parks Avatar answered Nov 07 '22 16:11

Brad Parks


Dropwizard is using Jersey 1.x. In Jersey you can throw a WebApplicationException to redirect a user.

Also see the answer here: https://stackoverflow.com/a/599131/360594

like image 26
Cemo Avatar answered Nov 07 '22 17:11

Cemo