Wednesday, June 6, 2012

RESTEasy Tutorial Part-2: Spring Integration


RESTEasy Tutorial Series

RESTEasy Tutorial Part-1: Basics

RESTEasy Tutorial Part-2: Spring Integration

RESTEasy Tutorial Part 3 - Exception Handling


RESTEasy provides support for Spring integration which enables us to expose Spring beans as RESTful WebServices.

Step#1: Configure RESTEasy+Spring dependencies using Maven.



Step#2: Configure RESTEasy+Spring in web.xml



Step#3: Create a Spring Service class UserService and update UserResource to use UserService bean.


Step#4: Same JUnit TestCase to test the REST Webservice described in Part-1.

Important Things to Keep in mind:
1. org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap Listener should be registered before any other listener.

2. You should configure resteasy.servlet.mapping.prefix <context-param> if the HttpServletDispatcher servlet url-pattern is anything other than /*

3. While using Spring integration set resteasy.scan to false or don't configure resteasy.scan parameter at all.
    Otherwise you may get REST Resource instances(UserResource) from RestEasy instead of Spring container. While running JUnit Tests I observed this random behavior.

4. You should register REST Resource as Spring bean by annotating with @Component or @Service.

6 comments:

  1. Thank you for this post! I was struggling with the integration of RESTEasy into Spring for about an hour because the official documentation at http://docs.jboss.org/resteasy/docs/2.3.4.Final/userguide/html/RESTEasy_Spring_Integration.html was just too abstract for me to understand.

    But copying the concepts from your example my code finally worked :-)

    ReplyDelete
  2. Its Perfect. The example is clear and the explanation is kept so Simple

    ReplyDelete
  3. Does anyone got a problem when trying to use propertyplaceholderconfigurer and ${something} into a bean's property but the configurer is instantiate only after the bean from some reason? Spring post processor??

    ReplyDelete
  4. Hi Siva,
    This is works perfectly fine in Apache Tomcat 7.0.54 sever but when the same war file deployed in JBoss AS 7 ....deployment gets fail with error message "Caused by: java.lang.NoSuchMethodError: org.jboss.resteasy.spi.InjectorFactory.createPropertyInjector(Ljava/lang/Class;Lorg/jboss/resteasy/spi/ResteasyProviderFactory;)Lorg/jboss/resteasy/spi/PropertyInjector;"
    Any idea why behaviour .?? am using Spring-3.1.0.release and resteasy-3.0.8.Final
    TIA

    ReplyDelete
    Replies
    1. Hi,
      I guess this is due to conflict of JBoss Jar versions with some jars you packaged in war file.
      Make sure you have provided scope for resteasy jars in pom.xml while deploying on JBoss.

      Delete