Spring & Restlet : 100% XML configuration?

2019-09-09 10:06发布

问题:

I'm working on a project where all configurations are kept in XML files. I'm about to begin on a smaller part of this project, and I'll be using Restlet for that. Basically, what I'd like to do is to create a few subclasses of ServerResource.

I could use annotations to specify which class methods accepts which HTTP methods, but since I'm using XML for everything else I'm a bit reluctant. Is there a way to map HTTP methods to class methods for Restlet resources?

The actual integration between Spring and Restlet is XML only (webcontext.xml) :

  <bean id="apiComponent" class="org.restlet.ext.spring.SpringComponent">
    <property name="defaultTarget" ref="apiAppliction" />
  </bean>

  <bean id="apiAppliction" class="com.company.api.ApiApplication">
    <property name="inboundRoot" ref="router" />
  </bean>

  <!-- Define the router -->
  <bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" />

  <!-- Define all the routes -->
  <bean name="/track/{trackId}" class="com.company.api.resource.TrackResource" scope="prototype" autowire="byName">
      <constructor-arg index="0" ref="serviceFactory"/> 
  </bean>
  <bean name="/album" class="com.company.api.resource.AlbumResource" scope="prototype" autowire="byName"/>  
  <bean name="/album/{albumId}/tracks" class="com.company.api.resource.AlbumTracksResource" scope="prototype" autowire="byName" />

Is there a way I can add to the above configuration and map HTTP methods to class methods?

回答1:

In fact, no. You can't define the mapping between HTTP methods and target server resource methods both with Restlet itself and with Spring configuration.

In fact, these are two different distinct parts of Restlet:

  • Definition of the routing to handle requests (authentication, filters, server resources and so on). This is done within the Restlet application class within the method createInboundRoot (your property inboundRoot for your bean apiAppliction).
  • Definition of the rounting for HTTP methods. Once the server resource to use is selected, this is done internally to the server resource explicitely (with tests within the method handleRequest) or using annotations.

In fact, you have the same thing with Spring MVC. You define the way to detect the controllers in the Spring container (auto-detection based on the annotation Controller for example) and then you configure your controllers using dedicated annotations.

In addition, Roger Stocker provides an improvment of Spring extension based on an XML namespace (see this link http://code4you.org/2013/07/spring-custom-xml-namespace-scheme-for-the-restlet-framework/). This contribution is currently in incubation to be integrated in the official Spring extension.

Hope it helps, Thierry