泽西岛没有捕获异常
Jersey not capturing an exception
我对 ExceptionMapper
的实现如下所示:
@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);
解决方法是在 ResourceConfig
中添加 this.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);
我对 ExceptionMapper
的实现如下所示:
@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);
解决方法是在 ResourceConfig
this.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);