Jerome Louvel <contact <at> noelios.com> writes:

> 
> Hi Takasho,
> 
> Did you have a look at the Spring extension javadocs in the 1.1 snapshot?
> There are some XML config snippets in them that you can use in your Spring
> configuration file, like:
> 
>  <bean id="application" class="org.restlet.ext.spring.SpringApplication">
>      <constructor-arg ref="component" />
> 
>      <property name="root">
>          ...
>      </property>
>  </bean>

Hi All,

I went through the Javadocs and the old wiki examples but I'm still getting
errors. It's probably just my understanding that's incorrect. Most of this code
is taken from Irfan Jamadar's example.

I have the following in my web.xml:

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
    
<context-param>
    <param-name>org.restlet.application</param-name>
    <param-value>org.example.rest.RestApplication</param-value>
</context-param>
   
<servlet>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>
        com.noelios.restlet.ext.servlet.ServerServlet
    </servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

In my applicationContext.xml I have:

<bean id="usersRestlet" class="org.example.rest.UsersRestlet" />
<bean id="userRestlet" class="org.example.rest.UserRestlet" />

<bean id="manager" class="org.example.rest.RestManager">
    <property name="resourceMappings">
        <bean class="java.util.HashMap">
            <constructor-arg>
            <map>
                 <entry key="/user/{user}">
                     <ref local="userRestlet" />
                 </entry>
                 <entry key="/user">
                     <ref local="usersRestlet" />
                 </entry>
            </map>
            </constructor-arg>
         </bean>
    </property>
</bean>

In my org.example.rest.RestApplication:

public class RestApplication extends Application {

        public RestApplication(Context context) {
        }

        @Override
        public Restlet createRoot() {
                Router router = new Router(getContext());
                SpringContext springContext = new SpringContext(getContext());
springContext.getXmlConfigRefs().add("war://WEB-INF/applicationContext.xml");
                springContext.refresh();
                RestManager manager = (RestManager)
                        springContext.getBean("manager");
                manager.init(router);
                return router;
        }
        public void handle(Request request, Response response) {
                try {
                     start();
                } catch (Exception ee) {
                        ee.printStackTrace();
                }
                super.handle(request, response);
        }
}

In my org.example.rest.RestManager I have:

public class RestManager {

        private Map<String, Restlet> resourceMappings =
                        new HashMap<String, Restlet>();

        public void init(Router router) {
                for (String key : resourceMappings.keySet()) {
                        router.attach(key, resourceMappings.get(key));
                }
        }
        public void setResourceMappings(
                HashMap<String, Restlet> resourceMappings) {
                this.resourceMappings = resourceMappings;
        }

}

The app is packaged as a .war file and deployed onto Jetty. For some reason I
am getting the following error on startup of Jetty:

INFO: No beans defined in application context [org.restlet.ext.spring.SpringCont
ext;hashCode=16278782]. Even though I am clearly calling
springContext.getXmlConfigRefs().add("war://WEB-INF/applicationContext.xml");.
The file exists at that location and contains my beans. If I move or delete the
file then the application really complains so it is seeing it.

Consequently it will not load the "manager" bean and the application fails to
start.

Sorry for the long post :)

Takasho.

Reply via email to