【问题标题】:Jersey not capturing an exception泽西岛没有捕获异常
【发布时间】:2017-11-13 18:17:43
【问题描述】:

我对@9​​87654321@ 的实现如下所示:

@Provider
public class CaptureUnhandledExceptions implements ExceptionMapper<Throwable>
{
    private static final Logger LOGGER = LogFactory.getLogger(CaptureUnhandledExceptions.class);

    @Override
    public Response toResponse(Throwable exception)
    {
        LOGGER.error("Unhandled exception", exception);
        ErrorMessage em = new ErrorMessage(50099, ExceptionUtils.getRootCause(exception).getMessage(),
                exception.getMessage());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(em).build();
    }
}

在我的 REST api 中,我尝试为我的 JSON 正文传递错误的值,但它引发了以下异常

Can not deserialize value of type com.abccompany.model.ConnectionEndpoint$Binding from String "RandomValue: value not one of declared Enum instance names: [POST, REDIRECT]
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3fce79f3; line: 16, column: 28] (through reference chain: com.abccompany.model.SpConnection["connection"]->com.abccompany.model.Connection["connectionendpoints"]->java.util.ArrayList[0]->com.abccompany.model.ConnectionEndpoint["endpointbinding"])

我错过了什么?我必须在ResourceConfig 中注册我的提供商吗?

在应用程序的 ResourceConfig 中

this.register(com.abccompany.utils.CaptureUnhandledExceptions.class);

【问题讨论】:

  • 您是否在日志文件中看到“未处理的异常”?您不需要注册自定义异常处理程序类。
  • 不...我这样做只是为了确保我没有遗漏任何东西
  • 尝试在你的 rest 方法中故意抛出一个新的 Exception("say something...") 看看会发生什么。
  • 我想我想通了。我必须在我的 ResourceConfig 中添加 this.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);

标签: java exception jax-rs jersey-2.0


【解决方案1】:

解决方法是在ResourceConfig 中添加this.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJs‌​onProvider.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多