在cxf Servlet上找不到资源类

时间:2011-03-09 10:02:30

标签: cxf jax-rs

我创建了一个非常简单的基于cxf非Spring的Servlet,它加载javax.ws.rs.Application类型。

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

  <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>
                com.mycomp.cxf.TestApplication
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

以下是申请表:

public class TestApplication extends Application
{
    private final Set<Class<?>> classes = new HashSet<Class<?>>();
    private final Set<Object> singletons = new HashSet<Object>();

    public TestApplication() throws ServletException 
    {

    }

    @Override
    public Set<Class<?>> getClasses() 
    {
        return classes;
    }

    @Override
    public Set<Object> getSingletons() 
    {
        return singletons;
    }
}

由于以下原因导致Servlet无法加载:“找不到资源类”,我不知道为什么在Application中有一组空类应该没问题。

这是完整的堆栈跟踪:

org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean                          - No resource classes found
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/test]  - StandardWrapper.Throwable
org.apache.cxf.service.factory.ServiceConstructionException
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:122)
    at org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet.createServerFromApplication(CXFNonSpringJaxrsServlet.java:304)
    at org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet.loadBus(CXFNonSpringJaxrsServlet.java:72)
    at org.apache.cxf.transport.servlet.AbstractCXFServlet.init(AbstractCXFServlet.java:78)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1172)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4371)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at com.springsource.osgi.webcontainer.tomcat.TomcatServletContainer.startWebApplication(TomcatServletContainer.java:120)
    at com.springsource.osgi.webcontainer.internal.StandardWebContainer$StandardWebApplication.start(StandardWebContainer.java:100)
    at com.springsource.osgi.webcontainer.extender.WebContainerBundleCustomizer.addingBundle(WebContainerBundleCustomizer.java:25)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerAdding(BundleTracker.java:440)
    at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:261)
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:233)
    at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:413)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:919)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:227)
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:149)
    at org.eclipse.osgi.framework.internal.core.Framework.publishBundleEventPrivileged(Framework.java:1349)
    at org.eclipse.osgi.framework.internal.core.Framework.publishBundleEvent(Framework.java:1300)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:380)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)
    at com.sap.core.js.deployer.watchservice.WARDeployer.deploy(WARDeployer.java:142)
    at com.sap.core.js.deployer.watchservice.FileSystemEventsListener.onChange(FileSystemEventsListener.java:26)
    at com.springsource.util.io.FileSystemChecker.notifyListeners(FileSystemChecker.java:182)
    at com.springsource.util.io.FileSystemChecker.check(FileSystemChecker.java:145)
    at com.sap.core.js.deployer.watchservice.WatchTask.run(WatchTask.java:29)
    at java.lang.Thread.run(Thread.java:679)
Caused by: javax.ws.rs.WebApplicationException
    at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:238)
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:85)
    ... 31 more
10:41:54,580 [ERROR] org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/test]  - Servlet /test threw load() exception
javax.ws.rs.WebApplicationException
    at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:238)
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:85)
    at org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet.createServerFromApplication(CXFNonSpringJaxrsServlet.java:304)
    at org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet.loadBus(CXFNonSpringJaxrsServlet.java:72)
    at org.apache.cxf.transport.servlet.AbstractCXFServlet.init(AbstractCXFServlet.java:78)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1172)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4371)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at com.springsource.osgi.webcontainer.tomcat.TomcatServletContainer.startWebApplication(TomcatServletContainer.java:120)
    at com.springsource.osgi.webcontainer.internal.StandardWebContainer$StandardWebApplication.start(StandardWebContainer.java:100)
    at com.springsource.osgi.webcontainer.extender.WebContainerBundleCustomizer.addingBundle(WebContainerBundleCustomizer.java:25)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerAdding(BundleTracker.java:440)
    at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:261)
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:233)
    at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:413)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:919)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:227)
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:149)
    at org.eclipse.osgi.framework.internal.core.Framework.publishBundleEventPrivileged(Framework.java:1349)
    at org.eclipse.osgi.framework.internal.core.Framework.publishBundleEvent(Framework.java:1300)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:380)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)
    at com.sap.core.js.deployer.watchservice.WARDeployer.deploy(WARDeployer.java:142)
    at com.sap.core.js.deployer.watchservice.FileSystemEventsListener.onChange(FileSystemEventsListener.java:26)
    at com.springsource.util.io.FileSystemChecker.notifyListeners(FileSystemChecker.java:182)
    at com.springsource.util.io.FileSystemChecker.check(FileSystemChecker.java:145)
    at com.sap.core.js.deployer.watchservice.WatchTask.run(WatchTask.java:29)
    at java.lang.Thread.run(Thread.java:679)

2 个答案:

答案 0 :(得分:3)

您需要通知您的环境您的服务类。首先需要添加服务类,否则只需在getSingletons()中返回null 所以只需更改现在空的构造函数即可添加服务类:

class TestApplication extends Application {
    public TestApplication() throws ServletException 
    {
      singletons.add( new com.example.MyService() )
    }
    ....
}

答案 1 :(得分:1)

好吧,我猜你不能拥有一组空的类,好像我添加一个然后它成功加载,例如:

public TestApplication() throws ServletException 
{
        singletons.add(someClass);
}