4

can anyone point me how to view my custom 404 page. I googled and tried implementing ExceptionMapper<RuntimeException> but this did not work fully. I'm using 0.8.1 version

My Exception mapper:

public class RuntimeExceptionMapper implements ExceptionMapper<NotFoundException> {
    @Override
    public Response toResponse(NotFoundException exception) {
        Response defaultResponse = Response.status(Status.OK)
            .entity(JsonUtils.getErrorJson("default response"))
            .build();
        return defaultResponse;
    }
}

This only works on incorrect APIs and not on resource calls

My Setup :

@Override
public void initialize(Bootstrap<WebConfiguration> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/webapp", "/", "index.html"));
}

@Override
public void run(WebConfiguration configuration, Environment environment) throws Exception {
    environment.jersey().register(RuntimeExceptionMapper.class);
    ((AbstractServerFactory) configuration.getServerFactory()).setJerseyRootPath("/api/*");

    // Registering Resources
    environment.jersey().register(new AuditResource(auditDao));
    ....
}

Now,

http://localhost:8080/api/rubish goes through overridden ExceptionMapper method http://localhost:8080/rubish.html results in default 404 page

How do i setup so that whenever unknown pages are requested dropwizard will show up a custom 404 page

I refereed this link for the exception mapper

Kedar Javalkar
  • 343
  • 1
  • 5
  • 22

2 Answers2

3

To configure a custom error page for any error, you can configure an ErrorPageErrorHandler in your Application like this:

@Override
public void run(final MonolithConfiguration config,
                final Environment env) {
    ErrorPageErrorHandler eph = new ErrorPageErrorHandler();
    eph.addErrorPage(404, "/error/404");
    env.getApplicationContext().setErrorHandler(eph);
}

Then create a resource like this:

@Path("/error")
public class ErrorResource {

    @GET
    @Path("404")
    @Produces(MediaType.TEXT_HTML)
    public Response error404() {
       return Response.status(Response.Status.NOT_FOUND)
                      .entity("<html><body>Error 404 requesting resource.</body></html>")
                      .build();
    }

Just in case you need it, here's the import for ErrorPageErrorHandler as well:

import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
craigching
  • 1,683
  • 2
  • 13
  • 12
  • Instead of returning HTML string i used, `Response.seeOther(new URI("/index.html")).build();` Thanks :) – Kedar Javalkar Aug 09 '17 at 10:29
  • Your comment reminds me that I had a further problem with this. The code above will indeed return the html provided, but the response status will be 200, not 404! I'll update with the correct code. – craigching Aug 09 '17 at 14:16
1

If I understand it right, what you want is to serve a custom 404 page for unmatched resource requests. To do this you could write a separate resource class and in it a separate resource method. This resource method should have an

@Path("/{default: .*}")

annotation. This resource method catches unmatched resource requests. In this method you can serve your own custom view.

Look at the below code snippet for clarity,

@Path("/")
public class DefaultResource {

  /**
   * Default resource method which catches unmatched resource requests. A page not found view is
   * returned.
   */
  @Path("/{default: .*}")
  @GET
  public View defaultMethod() throws URISyntaxException {
    // Return a page not found view.
    ViewService viewService = new ViewService();
    View pageNotFoundView = viewService.getPageNotFoundView();
    return pageNotFoundView;
  }

}

You can refer to this if you are unaware of how to serve static assets using dropwizard or ask me.

  • Using the `defaultResource` im able catch `http://localhost:8080/api/rubish`... I want to catch `http://localhost:8080/rubish.html`.. which is a html page request – Kedar Javalkar Feb 26 '16 at 05:01
  • Do you have a resource (or resource method) with a url pattern rubish.html ?? If there is already a registered resource with this url pattern rubish.html, default resource (mentioned in my answer) will not be able to catch that request. Only those requests which are uncaught by any other resource will be caught by default resource. – Sashidhar Thallam Feb 26 '16 at 10:26
  • All resources are prepended with `api`... rublish.html is a request for a static html page. it is not registered under resources – Kedar Javalkar Feb 29 '16 at 05:06