简体   繁体   English

用dropwizard捕获json处理异常?

[英]Catch json processing exception with dropwizard?

I have a dropwizard application with a global exception handler registered that implements ExceptionMapper<Throwable> . 我有一个注册了全局异常处理程序的dropwizard应用程序,该处理程序实现ExceptionMapper<Throwable> If I throw any runtime exception from within a resource the mapper gets hit. 如果我从资源中抛出任何运行时异常,则映射器将被命中。

However, in a test I am posting JSON to to a resource with a missing type discriminator for a jackson polymorphic type. 但是,在测试中,我将JSON发送到具有杰克逊多态类型缺少类型鉴别符的资源。 Jersey is returning a 400, and I can see that a JsonProcessingException is being thrown when stepping through the code, but the global mapper isn't getting hit. 泽西返回400,我可以看到JsonProcessingException步执行代码时抛出了JsonProcessingException ,但是全局映射器并未受到攻击。

I tried to see if there were any other exception mappers registered and to try and unregister them, by checking the environment.jersey().getResourceConfig().getSingletons() set and there were no other exception mappers registered. 我试图通过检查environment.jersey().getResourceConfig().getSingletons()设置来查看是否注册了其他异常映射器,并尝试注销它们,并且没有注册其他异常映射器。

I've also tried creating a specific handler for just that exception but no dice. 我也尝试为该异常但没有骰子创建一个特定的处理程序。 And even then that's kind of weird if I already have a global handler to catch all Throwable. 即使那时我已经有了一个全局处理程序来捕获所有Throwable,这还是很奇怪的。

Has anyone encountered this? 有人遇到过这个吗?

-- -

EDIT: 编辑:

looks like the dropwizard exception mapper does get registered and I somehow need to find out when in the lifecycle that happens and unregister it 看起来dropwizard异常映射器确实已注册,我不知何故需要找出发生在生命周期中的时间并将其取消注册

Yes, you do need to override the exception mapper for JsonProcessingException ( JsonProcessingExceptionMapper ). 是的,您确实需要重写JsonProcessingExceptionJsonProcessingExceptionMapper )的异常映射器。

looks like the dropwizard exception mapper does get registered and I somehow need to find out when in the lifecycle that happens and unregister it 看起来dropwizard异常映射器确实已注册,我不知何故需要找出发生在生命周期中的时间并将其取消注册

You can find out how to override an ExceptionMapper on this answer . 您可以在此答案中找到如何覆盖ExceptionMapper。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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