Saturday, June 23, 2012

Jersey, REST, MyBatis

I am new to REST and Jersey. Have been going through some online tutorials I googled and reading some books. I created a project and got everything setup. I created the database layer using MyBatis, which I have used in many other projects. I followed the tutorial exactly and got it working. As soon as I used MyBatis to create and populate the bean/pojo object, I would get this exception message:

SEVERE: A message body writer for Java class com.myproject.dto.Person$$EnhancerByCGLIB$$3806666a, and Java type class com.myproject.dto.Person, and MIME media type text/xml was not found

Not knowing much about how Jersey worked, with all the annotations, I initially thought it was a problem with one of the data types of the Person class, but soon discovered that was not it. I created two Person objects, one through MyBatis and one using the Java new operator. I noticed the one created with the Java new operator had member variables named CGLIB$BOUND set to true and CGLIB$CALLBACK_0 set with a value, but the Person object created by MyBatis did not. I googled "cglib$bound in mybatis" and found an entry that took me to the MyBatis source code with some error text that said "Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath."

So I decided to disable lazy loading in my MyBatis config file

<setting name="lazyLoadingEnabled" value="false"/>

and this fixed the problem!

Is this documented somewhere? Did I really have to spend 3 hours trying to track this down?

4 comments:

Unknown said...

Hi there
Could you provide me the source code of ur project? Im looking to do the same.

Brian Barnett said...

Sachin,
If you provide me an email address, I will email you a zip file of the project with one database table and all the supporting classes for it. The zip file is about 9MB. It may prove to be a good starting point for you. At a minimum, you may be able to leverage some of the code in your own project. It is an Eclipse-based project. Use at your own risk ;)

msidlee said...

Hi Brian,

Would you mind sending me a copy of your eclipse project please? I'm looking to do exactly the same and it always helps if you get a working example.

You can reach me at murali{dot}sid{at}gmail

Thanks in advance
Murali

khalil SGHAIER said...

Hi Brian,

Could you provide me a copy of your project ? I'm looking to do exactly the same and it always helps if you get a working example.

You can reach me at dev{dot}khalil{at}gmail

Thanks in advance
khalil