0

我正在开发一个所有配置都保存在 XML 文件中的项目。我即将开始这个项目的一小部分,为此我将使用 Restlet。基本上,我想做的是创建一些ServerResource.

我可以使用注释来指定哪些类方法接受哪些 HTTP 方法,但是由于我使用 XML 来处理其他所有内容,所以我有点不情愿。有没有办法将 HTTP 方法映射到 Restlet 资源的类方法?

Spring 和 Restlet 之间的实际集成仅是 XML (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" />

有没有办法可以添加到上述配置并将 HTTP 方法映射到类方法?

4

1 回答 1

1

事实上,没有。您无法使用 Restlet 本身和 Spring 配置定义 HTTP 方法和目标服务器资源方法之间的映射。

事实上,这是 Restlet 的两个不同的部分:

  • 定义处理请求(身份验证、过滤器、服务器资源等)的路由。这是在方法内的 Restlet 应用程序类中完成的(您的 beancreateInboundRoot的属性)。inboundRootapiAppliction
  • HTTP 方法的路由定义。一旦选择了要使用的服务器资源,这将在服务器资源内部显式地(使用方法中的测试handleRequest)或使用注释来完成。

事实上,你对 Spring MVC 也有同样的看法。您定义在 Spring 容器中检测控制器的方法(例如基于注解Controller的自动检测),然后使用专用注解配置控制器。

此外,Roger Stocker 提供了基于 XML 命名空间的 Spring 扩展改进(参见此链接http://code4you.org/2013/07/spring-custom-xml-namespace-scheme-for-the-restlet-framework/)。这个贡献目前正在孵化中,将被集成到官方的 Spring 扩展中。

希望它有帮助,蒂埃里

于 2015-02-03T09:11:36.577 回答