Domanda

I am using RESTEasy 2.0.1.GA on Jboss 4.3.0.GA. The interface that my resource class implemented looks like this:

@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;
}

When I set resteasy.scan=true in web.xml, it works fine. But it should be false with spring context. So now my web.xml is:

<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>

And I got this error:

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

It suggested that I missed @Path while actually I didn't. But when I put the annotation to the resource class it does work. I googled and found a solution in http://pilhuhn.blogspot.com/2011/06/seemingly-common-resteasy-error-and.html. It needs to add a @ApplicationPath annotation. Now my interface is in a third party lib so I can't modify it. Does anybody have some suggestions? Thanks in advance.

È stato utile?

Soluzione

It appears that you have not registered a Resteasy specific SpringContextLoaderListener. This is required for Spring to register scanned resource classes with Resteasy.

Below is an example web.xml configuration (this uses annotation configuration, but will also work with xml configuration):

<!-- 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>

Then you simply annotate your resource classes for Spring either with @Component or @Named as shown below:

@Named
@Path("/")
public class MyResource implements CwindRestfulService 
{
   //Stuff Goes Here...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top