0

I find myself working a grails application that is being deployed as a fat jar built by a custom plugin that uses dropwizard to configure jetty.

It seems as though dropwizard doesn't allow facilitate the use of a plain old web.xml or jetty.xml and instead everything is set by java config at startup (i.e. using com.yammer.dropwizard.config.Environment).

Am I missing something here? Is there some way to map a 404 back to a URL or any kind of web page I can override so that a Jetty 404 isn't the default.

(yes I'm aware I could do something with the load balancer to redirect 404s)

bensfromoz
  • 11
  • 1

1 Answers1

0

I dont know how it is in grails, but this helps in java with dropwizard 0.7.1 run() method:

ResourceConfig jrConfig = environment.jersey().getResourceConfig();
environment.jersey().register(new RestErrorsHandler(jrConfig ));

Create this class for the mapping of exceptions -> give back an individual response!

@Provider
public class RestErrorsHandler implements ExceptionMapper<Exception> {

/**
 * Deletes all ExpetionMappers.
 * 
 * @param jrConfig
 */
public RestErrorsHandler(
    ResourceConfig jrConfig)
{
    // Remove all of Dropwizard's custom ExceptionMappers
    Set<?> dwSingletons = jrConfig.getSingletons();
    List<Object> singletonsToRemove = new ArrayList<Object>();
    for (Object s : dwSingletons) {
            // Remove all Exception mappers
            if (s instanceof ExceptionMapper) {
                singletonsToRemove.add(s);
            }
    }
    for (Object s : singletonsToRemove) {
        jrConfig.getSingletons().remove(s);
    }
}

public Response toResponse(
    Exception exception)
{   
    //Handle different exceptions in another way
    if (exception.getClass().equals(JsonParseException.class)){
        Response response = RestErrorsHandler.generalResponse(exception);
        return response ;
    } else if(exception.getClass().equals(JsonParseException.class)){
        Response response = RestErrorsHandler.generalResponse(exception);
        return response ;
    } else if(exception.getClass().equals(Class.class)){
        Response response = RestErrorsHandler.generalResponse(exception);
        return response ;
    }

    //genral problem -> output default
    Response response = RestErrorsHandler.generalResponse(exception);
    return response ;

}

public static Response generalResponse(Exception exception)
{
    return Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN)
        .entity("if you just want to give back a string, but could also be default html page or whatever").build();
}



}
user3280180
  • 1,393
  • 1
  • 11
  • 27