RESTEasy - 在接口中标记@Path时资源类寄存器失败

时间:2014-02-20 10:47:49

标签: jboss jax-rs resteasy

我在Jboss 4.3.0.GA上使用RESTEasy 2.0.1.GA. 我的资源类实现的接口如下所示:

@Path("/")
public interface CwindRestfulService {
@GET
@Path("accounts/{email}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Account getAccountInfo(@PathParam("email") String email) throws Exception;
}

当我在web.xml中设置resteasy.scan = true时,它可以正常工作。但春天语境应该是假的。所以现在我的web.xml是:

<web-app>
    <display-name>Archetype RestEasy Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>    
    <context-param>
        <param-name>resteasy.resources</param-name>
        <param-value>com.cwind.rest.CwindRestfulServiceImpl</param-value>
    </context-param>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</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>/*</url-pattern>
    </servlet-mapping>
</web-app>

我收到了这个错误:

2014-02-20 18:10:23,303 ERROR [org.apache.catalina.core.ContainerBase]   Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
java.lang.RuntimeException: Class is not a root resource.  It, or one of its interfaces must be annotated with @Path: com.cwind.rest.CwindRestfulServiceImpl implements: com.cwind.rest.CwindRestfulService

这表明我错过了@Path而实际上我没有。但是当我将注释放到资源类时,它确实有效。我用Google搜索并在http://pilhuhn.blogspot.com/2011/06/seemingly-common-resteasy-error-and.html找到了解决方案。它需要添加@ApplicationPath注释。现在我的界面在第三方库中,所以我无法修改它。有人有什么建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您似乎尚未注册Resteasy特定SpringContextLoaderListener。这是Spring向Resteasy注册扫描资源类所必需的。

下面是一个示例web.xml配置(这使用注释配置,但也适用于xml配置):

<!-- Spring Configuration -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>com.myapp.SpringConfig</param-value>
</context-param>

<!-- RESTEasy Configuration -->
<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/</param-value>
</context-param>

<!-- RESTEasy <-> Spring Connector (Needed so that RESTEasy has access to Spring managed beans!) -->
<listener>
    <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>

<!-- RESTEasy HTTP Request Processor Servlet -->
<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

然后,您只需使用@Component@Named注释Spring的资源类,如下所示:

@Named
@Path("/")
public class MyResource implements CwindRestfulService 
{
   //Stuff Goes Here...
}
相关问题