Я использую джерси 2.29.
Я написал класс сопоставления исключений, чтобы перехватить все исключения. Но только некоторые из исключений попадают в ловушку.
Например, когда я выбрасываю свои пользовательские исключения, например BusinessException, ConfigException, тогда вызывается картограф. Но когда есть внутренняя ошибка сервера, она не вызывается.
@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
private static final Logger logger = LogManager.getLogger(GenericExceptionMapper.class);
@Override
public Response toResponse(Throwable exception) {
ErrorMessage errorMessage = null;
int errorCode = Status.INTERNAL_SERVER_ERROR.getStatusCode();
if (exception instanceof BusinessException) {
logger.debug("BusinessException:", exception);
BusinessExceptionType businessExceptionType = ((BusinessException) exception).getErrorCode();
switch (businessExceptionType) {
case NOT_FOUND:
errorCode = Status.NOT_FOUND.getStatusCode();
break;
case NOT_AUTHORISED:
errorCode = Status.UNAUTHORIZED.getStatusCode();
break;
default:
break;
}
} else if (exception instanceof ConfigException) {
logger.fatal("ConfigException:", exception);
} else if (exception instanceof WebApplicationException) {
logger.error("WebApplicationException:", exception);
Response response = ((WebApplicationException) exception).getResponse();
errorCode = response.getStatus();
}
else {
logger.fatal("GenericException:", exception);
}
errorMessage = new ErrorMessage(errorCode, exception.getMessage());
return Response.status(errorCode).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();
}
}
Для Business Exception ошибка является правильной JSON:
{
"errorCode": 404,
"errorMessage": "Not found"
}
Для любого другого сервера выдайте сообщение об ошибке не JSON:
<code><!doctype html>
<html lang="en">
<head>
<title>HTTP Status 500 – Internal Server Error</title>
<style type="text/css">
body {
font-family: Tahoma, Arial, sans-serif;
}
h1,
h2,
h3,
b {
color: white;
background-color: #525D76;
}
h1 {
font-size: 22px;
}
h2 {
font-size: 16px;
}
h3 {
font-size: 14px;
}
p {
font-size: 12px;
}
a {
color: black;
}
.line {
height: 1px;
background-color: #525D76;
border: none;
}
</style>
</head>
<body>
<h1>HTTP Status 500 – Internal Server Error</h1>
<hr class="line" />
<p><b>Type</b> Exception Report</p>
<p><b>Message</b> A MultiException has 3 exceptions. They are:</p>
<p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.
</p>
<p><b>Exception</b></p>
<pre>javax.servlet.ServletException: A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=AccountService,parent=AccountResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1554545299)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of in.pnb.ebanking.ws.resource.AccountResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on in.pnb.ebanking.ws.resource.AccountResource
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:408)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Причина
A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=AccountService,parent=AccountResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1554545299)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of in.pnb.ebanking.ws.resource.AccountResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on in.pnb.ebanking.ws.resource.AccountResource
org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:65)
org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:226)
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)
org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:59)
org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:754)
org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:717)
org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:687)
org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.getInstance(AbstractHk2InjectionManager.java:160)
org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.getInstance(ImmediateHk2InjectionManager.java:30)
org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:105)
org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:260)
org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:51)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:86)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:69)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:38)
org.glassfish.jersey.process.internal.Stages.process(Stages.java:173)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:245)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.process(Errors.java:244)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:392)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Причина
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=AccountService,parent=AccountResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1554545299)
org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:51)
org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:188)
org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:211)
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)
org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:59)
org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:754)
org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:717)
org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:687)
org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.getInstance(AbstractHk2InjectionManager.java:160)
org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.getInstance(ImmediateHk2InjectionManager.java:30)
org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:105)
org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:260)
org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:51)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:86)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:89)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:69)
org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:38)
org.glassfish.jersey.process.internal.Stages.process(Stages.java:173)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:245)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.process(Errors.java:244)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:392)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Примечание Полная трассировка стека по причине root доступна в журналах сервера.
Apache Tomcat / 9.0.30
Ожидаемое:
{
"errorCode": 500,
"errorMessage": "There was an error processing your request."
}