Last weekend I tried to use Jersey in an enterprise application to make some RESTful web services available. The idea was to generate data to be plot by flash charts. So, I created a new web module in addition to the already existent one. I thought it was better to keep it in a separate module to avoid changing the original web application, since it still uses Struts as web framework. Before adding any RESTful web service, I just created the web application and deployed it successfully as an additional enterprise module. I'm happy about that because I will use the same strategy to migrate the web module from Struts to JSF, keeping the Struts version working in parallel to the JSF version, both sharing the same business logic.
I use Netbeans because it makes my life easier. The Jersey module is available in the 6.1 version and for download in the update center for previous versions. Since installed, you may click with the right button on a web project to see the context menu and select “New ... / RESTful Web Services from Patterns” or “New ... / RESTful Web Services from Entity classes”. Since I'm not implementing entity classes in this module, I selected the one from patterns. It will start a wizard to prepare the application for REST and generate the initial class skeleton to start working.
I followed those steps and the RESTful web service was created successfully. However, when I start to deploy the whole application again, an obscure error appears in the Glassfish output log insistently. In such situation I tried everything: clean, build, clean and build, remove the module and deploy it separately, but nothing was enough to solve the problem below:
LifecycleException: java.lang.NoSuchMethodError: org.objectweb.asm.ClassReader.accept(Lorg/objectweb/asm/ClassVisitor;I)
(...)
Caused by: java.lang.IllegalStateException:
ContainerBase.addChild:
start: LifecycleException:
java.lang.NoSuchMethodError: org.objectweb.asm.ClassReader.accept(Lorg/objectweb/asm/ClassVisitor;I)
I checked if I was using the libraries appropriately, if they were correctly wrapped and if there was any library conflict. Yes, there is a library conflict with Hibernate, the persistence framework I am using as a JPA implementation. Jersey uses the ASM library, which is also used by Hibernate, but on different versions. Hibernate uses version 1.5.3 and Jersey uses 3.5.1. A big difference! We can not just delete the old version and put the new one there because they are architecturally different. Version 1.5.3 has an "attrs" package while 3.5.1 doesn't have it but a "signatures" package. Changing the version can cause other problems and I'm not expecting them afterwards. So, how do I solve it consistently?
The ASM package is needed by the cglib package, which is part of the Hibernate libraries. If we remove that package, Jersey will work correctly, but Hibernate will stop working. To solve this conflict use cglib-nodep.jar instead of cglib.jar and keep ASM version 3.x with Jersey. cglib-nodep.jar includes some ASM classes demanded by cglib.jar, changing the package name to avoid any class conflict.
The ASM library is a Java bytecode manipulation and analysis framework. According to its website "(...) it can be used to modify existing classes or dynamically generate classes, directly in binary form. Provides common transformation and analysis algorithms to easily assemble custom complex transformations and code analysis tools". ASM is used by many products like AspectJ, Oracle TopLink, JRuby, and many others. It can not be simply ignored by frameworks because it is a matter of flexibility. The best alternative is always to investigate the unexpected problem and claim for a better error presentation on the JVM.
Thanks for this post, really helped me. For no reason I can think of, Netbeans6.1 decided to add jersey to my web app, causing all the errors you stated. I just did an upgrade so maybe that triggered it, who knows, either ways, big thanks !!
ReplyDeleteYou should try using TopLink for your JPA implementation. Even though TopLink uses ASM, it won't conflict with other layers that use different versions of it.
ReplyDeleteHi Mike! Actually, I started using Toplink Essentials at the beginning, but when errors occur they are not so clear like the ones generated by Hibernate. I moved to Hibernate also because Toplink is not in the long term plans of the Glassfish team, since they are adopting Eclipse Link instead of Toplink on Glassfish V3.
ReplyDeleteToplink Essentials is, actually, not so good like the one sold by Oracle. I think it was just a marketing strategy from Oracle.
Thank you very much for this help, i solved my problem this way.
ReplyDelete;)
I had the same issue. resolved by using cglib-nodep. thanks much for posting this solution.
ReplyDeleteSame problem with Netbeans 6.5.
ReplyDeleteThanks much for this solution
Hello -
ReplyDeleteI too faced the same issue when using hibernate/jersey/jboss. What I did was download asm-3.0.jar file and put it in the jboss lib folder. and then restart jboss. and it solved the issue !
Hello!
ReplyDeletevery good solution. But there is a problem that any jsp or html page is not dispaying in the application where i had replaced the cglib and asm jars. The error http 404 is encountered when i tried to load any jsp or html. When I replaced back the orignal versions of cglib and asm, the html and jsp are visible correctly but jersey stops working. I am using tomcat 6.x server
Hello Anonymous,
ReplyDeleteUsually when this error happens you probably had a problem to deploy your application. HTTP 404 means nothing available on the server for the requested URL. So, your problem could be well described in the log files of Tomcat. Only with this logs is possible to figure out what is going on there.
I had the same Issue . resolved by using the cglib-nodep-2.2.jar. Thanks for posting this issue
ReplyDeleteHad the same, greetings from Luxembourg
ReplyDeleteGood article and fixed worked for me too. Thanks for taking the time to document this.
ReplyDeleteAwesome!
ReplyDeleteI ran into the same problem with NetBeans 6.5, where I was using Hibernate JPA as well as Jersey.
I just replaced cglib-2.1.3.jar [in the Hibernate distribution bundled with NetBeans] with cglib-nodep-2.2.jar], and it got rid of all my asm run-time exceptions.
Thanks for the solution!
It helped
ReplyDeleteThanks much
Fantastic! Worked with the app I had created with NetBeans 6.7.1. I removed the asm, asm-attr and cgilib jars from the Hibernate JTA group of libraries, and manually added cgilib-nodep which I downloaded from sourceforge. Everything is now working on Tomcat 6. Thanks for posting this.
ReplyDeletewhile using the cglib-nodep-2.2.jar and JpaDaoSupport in spring , I am getting a new exception as java.lang.ClassCastException: $Proxy29 cannot be cast to. But this jar works fine for jpaTemplate.
ReplyDeleteThanks for the detailed information on the conflict and a solution to resolve it. Your solution worked like a charm. I hope one day Jersey and Hibernate can play nice but for now this works.
ReplyDeleteThanks a ton !! This was really useful. It was like a nightmare resolved and that too just before my project deadlines.
ReplyDeleteHey !
ReplyDeleteI really like to thank you for this solution.
You saved me a lot of time !!!
Thanks again !!!
Thanks so much! This resolved my problem after much head banging
ReplyDeleteAdding to the love fest from Costa Rica. Thanks, for the nice, clean solution. Peace.
ReplyDeleteThanks a lot for this information. It helped me debug my problem. Users facing this problem should also check out the solution provided on:
ReplyDeletehttp://forum.springsource.org/showthread.php?t=26713
This link has some good script to resolve this problem for maven 2 users.
Thank you very much!!!
ReplyDeleteI know you have heard it before but thanks again
ReplyDeleteOne more time: Thank You!!
ReplyDeleteYou saved my day ! Thank you very much !
ReplyDeleteMatthieu from France
Many many many thanks!!
ReplyDeletePhilippe
Thx for your help. That is what I need to continiou with my job
ReplyDeleteThanks a lot.
ReplyDeleteThanks for you dedication to bring this priceless information to us! I was strugling with my Maven + Hibernate + Jersey long time. =)
ReplyDeleteBest Regards
Paulo Miguel Almeida
WAS HELPFUL
ReplyDeleteThank you for a Belgian for your help ;).
ReplyDeleteI use Netbeans 7.0. I have removed the file /hibernate/cglib-2.1.3.jar. I have found /cglib-2.2 that I'have renamed by /cglib-nodep-2.2.jar and have added.
John[BE]
Maybe we can have a beer some day because I'm also hosted in Belgium ;)
ReplyDeleteThank you! You've just made my day!
ReplyDelete