Dropwizard - how to make server side redirect from view?

I am new to Drop Wizard and would like to redirect from viewing to another server in my application.

Does DropWizard launch this general task?

eg.

@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; } } 
+6
source share
2 answers

Here is a simple code example that actually redirects using a WebApplicationException. That way, you can put this into your opinion or in your resource and just drop it every time.

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

You can also simply return your resource either to the view or in response to a redirect:

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

Dropwizard uses Jersey 1.x. In Jersey, you can throw a WebApplicationException to redirect the user.

Also see the answer here: fooobar.com/questions/32829 / ...

+1
source

All Articles