【发布时间】:2011-07-25 15:16:16
【问题描述】:
- RESTEasy 2.0.1GA
- Java 1.6
- 春季 3.0.3
我已尽我所能,但无法完全确定正在发生的事情。我有一个 Spring MVC 应用程序,但是我希望在 Spring MVC 应用程序之外 有一些可用的 RESTEasy 端点,但在同一个容器中,最终能够连接到同一个 bean。
作为第一步,我只是尝试在容器内建立 RESTEasy,为来自 Spring 配置的 bean 的请求提供服务。我已经尝试了说明中的样板,也尝试了手动设置,但无济于事。
豆
@Resource
@Path("/")
public class NeighborComparison {
private String foo;
@GET @Path(value="customer") @Produces("text/plain")
public String getNeighborComparison() {
return "foo";
}
}
web.xml
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/api</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<!-- NOT configuring SpringContextLoaderListener because I declare my own, so if I do, everything
blows up, plus all it actually does is sanity check configuration -->
<listener>
<listener-class>com.example.MyCustomContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
applicationContext.xml
<bean id="resteasy.providerFactory" class="org.jboss.resteasy.spi.ResteasyProviderFactory"
factory-method="getInstance">
</bean>
<bean id="resteasy.dispatcher" class="org.jboss.resteasy.core.SynchronousDispatcher">
<constructor-arg ref="resteasy.providerFactory"/>
</bean>
<bean id="resteasy.spring.bean.processor" class="org.jboss.resteasy.plugins.spring.SpringBeanProcessor">
<description>
Add Resources and @Providers to the appropriate places
in Resteasy's infrastructure
</description>
<constructor-arg ref="resteasy.dispatcher"/>
</bean>
<bean id="neighborComparison" class="opower.api.customer.neighbor_comparison.NeighborComparison">
</bean>
根据文档,我所要做的就是“通过分配 org.jboss.resteasy.plugins.spring.SpringBeanProcessor 的实例手动注册 RESTeasy BeanFactoryPostProcessor”。我相信这个 spring 配置可以做到这一点。
Jetty 启动,应用上下文启动,没有任何问题。应用程序正常工作,但是当我
> curl -H"Accept: text/plain" localhost:8080/ei/api/customer
(“ei”是应用程序上下文)。日志显示(只有这个):
2011-03-29 16:44:24,153 DEBUG [qtp-575315405-0] [EI] [] [asy.core.SynchronousDispatcher] PathInfo: /customer
2011-03-29 16:44:24,156 DEBUG [qtp-575315405-0] [EI] [] [asy.core.SynchronousDispatcher] Failed executing GET /customer
org.jboss.resteasy.spi.NotFoundException: Could not find resource for relative : /customer of full path: http://localhost:8080/ei/api/customer
即使我可以说服 RESTEasy 向我展示映射,它似乎只是没有发现我的 bean。
如果我通过 resteasy.resources 上下文参数显式映射它,它可以工作,但显然无法访问自动连接的 Spring bean。
还有什么我可以尝试的吗?我在整个 RESTEasy 代码库上有调试日志,但没有收到任何消息。我还确认 Spring 实际上正在创建我的 bean,所以只是 RESTEasy 没有找到它。
【问题讨论】:
-
Spring MVC 有自己的 REST 风格的框架。为什么不直接使用它,避免整个问题?
-
与 JAX-RS 相比,Spring MVC “rest”框架非常弱且功能匮乏
-
如果您的代码示例就是这样的例子,那么它很容易做到。
-
戴夫,你能分享一下正确的解决方案吗?谢谢。
标签: java spring spring-mvc resteasy