users@jersey.java.net

[Jersey] How to catch MessageBodyProviderNotFoundException.

From: <marceloverdijk_at_gmail.com>
Date: Thu, 22 Aug 2013 20:16:56 +0000 (UTC)

When I return a response with an entity which cannot be a converted I
get a MessageBodyProviderNotFoundException.
Which makes sense of course.

The problem is want to catch this nicely and give a custom response
back, not just a 500.

I tried configuring a global exception manager like
public class GlobalExceptionMapper implements
ExceptionMapper<Exception> {
but this won't catch the MessageBodyProviderNotFoundException. I think
the MessageBodyProviderNotFoundException happens at a later stage...

Note that my global exception manager works when I throw a
MessageBodyProviderNotFoundException manually inside a resource action.


Is there any other way to catch the
MessageBodyProviderNotFoundException?

Here is btw the full stacktrace I get:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundExcept
ion: MessageBodyWriter not found for media type=application/json,
type=class org.mycomp.todo.model.User, genericType=class
org.mycomp.todo.model.User.
        at
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$Termina
lWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
        at
org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed
(WriterInterceptorExecutor.java:139)
        at
org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundW
riteTo(JsonWithPaddingInterceptor.java:103)
        at
org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed
(WriterInterceptorExecutor.java:139)
        at
org.glassfish.jersey.server.internal.MappableExceptionWrapperIntercepto
r.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
        at
org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed
(WriterInterceptorExecutor.java:139)
        at
org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(Messag
eBodyFactory.java:1005)
        at
org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(Serve
rRuntime.java:515)
        at
org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(Ser
verRuntime.java:362)
        at
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRunti
me.java:352)
        at
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:240)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        at
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestSc
ope.java:318)
        at
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:21
1)
        at
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandle
r.java:982)
        at
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:359
)
        at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.
java:372)
        at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.
java:335)
        at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.
java:218)
        at
org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
        at
org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390
)
        at
org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:
216)
        at
org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182
)
        at
org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765
)
        at
org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
        at
org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandle
rCollection.java:230)
        at
org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.ja
va:114)
        at
org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152
)
        at org.mortbay.jetty.Server.handle(Server.java:326)
        at
org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
        at
org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConn
ection.java:926)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
        at
org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
        at
org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
        at
org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java
:410)
        at
org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.jav
a:582)
> Building > :jettyRun > Running at http://localhost:8080/todo