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

Comments:

Brilliant!, totally works, thanks it helped me a lot

Posted by Miguel Rueda on November 09, 2014 at 04:04 PM PST #

Just great !!.
Thanks !!

Posted by guest on November 19, 2014 at 10:47 AM PST #

Thanks, it works!

Posted by Marek on March 02, 2015 at 07:18 AM PST #

I'm new to Java and stuff like this drives me nuts. :) Thanks so much for posting it, it's helped me keep moving.

Posted by TimF on March 13, 2015 at 09:30 AM PDT #

Thanks Dude !! That was a lifesaver!!

Posted by guest on April 21, 2015 at 06:27 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
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
« April 2015
SunMonTueWedThuFriSat
   
4
5
11
12
28
29
30
  
       
Today