问题描述
对于我的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?
编辑注意:我已经尝试过一些不太通用的方法,在这种情况下,我将使用InvalidFormatException而不是JsonMappingException来调用映射器.但是我还是不明白,因为InvalidFormatException扩展了JsonMappingException并应该被调用
EDITNote: 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)).
要覆盖此行为,只需禁用映射器即可:
-
使用XML:
<init-param> <param-name>jersey.config.server.disableAutoDiscovery</param-name> <param-value>true</param-value> </init-param>
Using XML:
<init-param> <param-name>jersey.config.server.disableAutoDiscovery</param-name> <param-value>true</param-value> </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.
这篇关于泽西岛无法捕获任何杰克逊异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!