Skip to content
Sergii Kabashniuk edited this page Mar 13, 2015 · 3 revisions

Integration !EverRest with Spring web framework

Spring

First you need configure Spring DispatcherServlet in your web.xml:

<servlet>
      <servlet-name>everrest</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
      <servlet-name>everrest</servlet-name>
      <url-pattern>/*</url-pattern>
</servlet-mapping>

Not need add any others in web.xml since we will use Spring to set up components of !EverRest framework and our JAX-RS components.

Since we set servlet-name as everrest we need create file everrest-servlet.xml in the WEB-INF directory of your web application with content as following:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="everrest.resources" class="org.everrest.core.impl.ResourceBinderImpl" />
   <bean id="everrest.providers" class="org.everrest.core.impl.ApplicationProviderBinder" />
   <bean class="org.everrest.spring.SpringComponentsLoader">
      <constructor-arg ref="everrest.resources" />
      <constructor-arg ref="everrest.providers" />
   </bean>
   <bean id="everrest.dependencies" class="org.everrest.spring.SpringDependencySupplier" />
   <bean id="everrest.handler.mapping" class="org.everrest.spring.EverrestHandlerMapping">
      <constructor-arg ref="everrest.resources" />
      <constructor-arg ref="everrest.providers" />
      <constructor-arg ref="everrest.dependencies" />
   </bean>
   <bean id="everrest.handler" class="org.everrest.spring.EverrestHandlerAdapter" />

   <!-- Add our JAX-RS components. -->
   <bean scope="prototype" class="org.everrest.example.MyResource" />
   <bean class="org.everrest.example.MyProvider" />
</beans>

Note: We set scope attribute to prototype since we want to have new instance of MyResource for each request. By default components of Spring container are singletons.

That is all. We do not need write any additional code to get it work.

Example of !EverRest and Spring integration can be found at subversion repository, see project everrest/everrest-integration/everrest-integration-spring-sample. You can simple run it with command: mvn jetty:run. See details how-to try example in README.html in correspond project.