What is the correct way to configure custom mapper for WebApplicationException?

让人想犯罪 __ 提交于 2019-12-07 14:47:59

问题


I've created class that implements implements ExceptionMapper<WebApplicationException> and registered it by environment.addProvider(new WebApplicationExceptionMapper());. My custom mapper works but only for some exceptions extended from WebApplicationException. For example it doesn't work for ConflictException and it also doesn't work for my custom exceptions with following constructor:

public ConflictException(URI location, Object entity) {
    super(Response.status(Response.Status.CONFLICT).location(location).entity(entity).build());
}

It will work if I'll remove super(Response.status..... This is very strange and I can't explain this. I'm not sure is it Jersey or Dropwizard behaviour.

What is correct way to configure mapper for all WebApplicationException and subclasses? Can you explain problems that I have?


回答1:


May be a little too late, but found this to work.

for dropwizard 0.8 and above


in the yaml configuration

server:
    registerDefaultExceptionMappers: false

This would disable the DWs default exception mapper, then add your own exception mappers

// Register the custom ExceptionMapper(s)
    environment.jersey().register(new RestErrorsHandler());

Source: github issue




回答2:


This post shows how to create custom ExceptionMappers:

http://gary-rowe.com/agilestack/2012/10/23/how-to-implement-a-runtimeexceptionmapper-for-dropwizard/

Perhaps this post could also help, if you having problems with your custom exceptionMappers: http://thoughtspark.org/2013/02/25/dropwizard-and-jersey-exceptionmappers/



来源:https://stackoverflow.com/questions/21482030/what-is-the-correct-way-to-configure-custom-mapper-for-webapplicationexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!