Friday Jun 20, 2014

Fixed: MessageBodyReader not found for media type

I've been plagued with this error over the past few days:

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class com.mycompany.customerclientmaven.Customer, genericType=class com.mycompany.customerclientmaven.Customer.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:173)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:988)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:833)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768)

This is what you get when you're running a RESTful web service client, e.g., you've created RESTful web services that expose customers, etc. Now you want to access those services. 

In your client code you have statements like this:

resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);

Everything compiles fine. Then you run the application and see the stack trace above.

Much googling later brought me to this which works out of the box:

http://code.google.com/p/genson

Genson is a data-binding and streaming library for JSON. It implements the extension points MessageBodyReader and MessageBodyWriter of JAX-RS and declares its implementation with Java's service loader mechanism. That in turn allows Jersey (and other RESTful web service libraries) to automatically detect Genson and use it for JSON parsing/writing.

For Maven, it's as simple as adding this, otherwise download the JAR and add it to your classpath yourself.

<dependency>
    <groupId>com.owlike</groupId>
    <artifactId>genson</artifactId>
    <version>0.99</version>
</dependency

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« June 2014 »
SunMonTueWedThuFriSat
14
15
19
28
29
     
       
Today