• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

泽西岛没办法捕捉到任何杰克逊异常

用户头像
it1352
帮助1

问题说明

对于我的 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

正确答案

#1

遇到了同样的问题.
问题是 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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcbjifb
系列文章
更多 icon
同类精品
更多 icon
继续加载