简体   繁体   中英

How to view custom 404 page in Dropwizard

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

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM