泽西岛无法捕捉到任何杰克逊异常 [英] Jersey unable to catch any Jackson Exception

查看:20
本文介绍了泽西岛无法捕捉到任何杰克逊异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 REST api,我使用 jersey 和 ExceptionMapper 来捕获全局异常.它适用于我的应用程序抛出的所有异常,但我无法捕获杰克逊抛出的异常.

For my REST api I'm using jersey and ExceptionMapper to catch global exceptions. It works well all the exception my app throws but I'm unable to catch exception thrown by jackson.

例如,我的一个端点接受一个包含枚举的对象.如果请求中的 Json 具有不在枚举球衣中的值,则抛出此异常

For example one of my endpoint accept an object that contains an enum. If the Json in the request has a value that is not in the enum jersey throw this exception back

Can not construct instance of my.package.MyEnum from String value 'HELLO': value not one of declared Enum instance names: [TEST, TEST2]
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@5922e236; line: 3, column: 1] (through reference chain: java.util.HashSet[0]->....)

即使我已经创建了这个映射器

Even though I have created this mapper

@Provider
@Component
public class JacksonExceptionMapper implements ExceptionMapper<JsonMappingException> {
  @Override
  public Response toResponse(JsonMappingException e) {
    ....
  }
}

代码永远不会到达这个映射器.

The code never reach this mapper.

为了捕获这些异常,我们需要做些什么吗?

Is there anything we need to do in order to catch these exceptions?

编辑注意:我只是尝试不那么通用,而不是 JsonMappingException 我使用 InvalidFormatException 在这种情况下调用映射器.但是我还是不明白,因为 InvalidFormatException 扩展了 JsonMappingException 并且也应该被调用

EDIT Note: I have jus tried being less general and instead of JsonMappingException I use InvalidFormatException in this case the mapper is called. But I still don't understand because InvalidFormatException extends JsonMappingException and should be called as well

推荐答案

遇到了同样的问题.
问题是 JsonMappingExceptionMapper 在您的映射器之前启动.

实际的异常是类com.fasterxml.jackson.databind.exc.InvalidFormatException,映射器定义了com.fasterxml.jackson.jaxrs.base.JsonMappingException,所以它更具体到异常.
你看,Jersey 的异常处理程序会寻找最准确的处理程序(参见 org.glassfish.jersey.internal.ExceptionMapperFactory#find(java.lang.Class, T)).

Had the same problem.
The problem is that JsonMappingExceptionMapper kicks in before your mapper.

The actual exception is of class com.fasterxml.jackson.databind.exc.InvalidFormatException and the mapper defines com.fasterxml.jackson.jaxrs.base.JsonMappingException, so it's more specific to the exception.
You see, Jersey's exception handler looks to find the most accurate handler (see org.glassfish.jersey.internal.ExceptionMapperFactory#find(java.lang.Class, T)).

要覆盖此行为,只需禁用映射器即可:

  1. 使用 XML:<代码><初始化参数><param-name>jersey.config.server.disableAutoDiscovery</param-name><参数值>true</参数值></init-param>

使用代码:resourceConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true); 其中 resourceConfig 的类型为 org.glassfish.jersey.server.ServerConfig.

Using code: resourceConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true); where resourceConfig is of type org.glassfish.jersey.server.ServerConfig.


您也可以编写自己的特定映射器:


You can also write your own specific mapper:

public class MyJsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException>

但我认为这是过度杀戮.

But I think it's an over kill.

这篇关于泽西岛无法捕捉到任何杰克逊异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆