pplicationException配置自定义映射器的正确方法

pplicationException配置自定义映射器的正确方法

本文介绍了为WebApplicationException配置自定义映射器的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了实现实现ExceptionMapper< WebApplicationException> 的类,并通过 environment.addProvider(new WebApplicationExceptionMapper()); code>。我的自定义映射器工作,但仅对于从 WebApplicationException 扩展的一些异常。例如,它不适用于 ConflictException ,它也不适用于以下构造函数的自定义异常:

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

如果我删除 super Response.status .... 这是非常奇怪的,我不能解释这一点,我不知道是否泽西 Dropwizard 行为。



为所有配置映射器的正确方法WebApplicationException 和子类?你可以解释我有什么问题吗?

解决方案

可能有点太晚了,但发现这个



在yaml配置中

 服务器:
registerDefaultExceptionMappers:false

这将禁用DWs默认异常映射器,然后添加自己的异常映射器

  //注册自定义ExceptionMapper 
environment.jersey()。register(new RestErrorsHandler ()); $ b $资料来源:


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?

解决方案

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


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

这篇关于为WebApplicationException配置自定义映射器的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 18:10