我的DataNotFoundException是这样的:
public class DataNotFoundException extends RuntimeException
{
private static final long serialVersionUID = 240618041975267964L;
public DataNotFoundException( String message )
{
super( message );
}
}
我用ExceptionMapper完美地映射它
@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>
{
@Override
public Response toResponse( DataNotFoundException e )
{
ErrorMessage errorMessage = new ErrorMessage( e.getMessage(), 404, "http://www.google.com" );
return Response.status( Response.Status.NOT_FOUND ).entity( errorMessage ).build();
}
}
当抛出此异常时,它会给我内部错误,这意味着它没有映射。
'<pre>java.lang.NullPointerException
org.srdc.arda.playground.service.MessageService.getMessage(MessageService.java:26)
org.srdc.arda.playground.resources.MessageResource.getMessage(MessageResource.java:37)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
&#39;
但如果我用这样的事情映射每个例外:
@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable>
{
@Override
public Response toResponse( Throwable throwable )
{
ErrorMessage errorMessage = new ErrorMessage( throwable.getMessage(), 500, "www.hotmail.com" );
return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).entity( errorMessage ).build();
}
}
这很好。
我通过这种方法抛出异常:
public Message getMessage( long i )
{
Message message = messages.get( i );
if ( message == null )
{
throw new DataNotFoundException( "Message with id " + message.getId() + " not found." );
}
return message;
}
这背后可能是什么原因?
提前致谢。