Tomcat + Jersey生成org.glassfish.jersey.server.ContainerException:java.lang.NoClassDefFoundError

时间:2016-03-10 09:47:35

标签: java rest tomcat jersey noclassdeffounderror

我知道此问题已经发布在此处,但我似乎无法找到问题的答案..

我试图从" ProjectA"调用POJO中的方法(" PojoClass"),它将从导入的类中实例化一个对象(" ForeignClass")然后调用它自己的方法之一..但我保持获取有关ForeignClass的NoClassDefFoundError错误。如果我将PojoClass转换为Main,它会找到没有问题的ForeignClass并实例化该对象。应该以某种方式将ForeignClass导入ProjectA的WEB-INF / lib中以供Jersey识别吗?

ProjectA' web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>PackageA</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>PackageA</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

PojoClass:

import PackageB.ForeignClass;



@Path("/admin")
public class PojoClass {


    @GET
    public void test(){

        ForeignClass fc = new ForeignClass();
        fc.testRest(); // << this method just prints "test"

    }

}

然后http://localhost:8080/ProjectA/rest/admin 产生:org.glassfish.jersey.server.ContainerException:java.lang.NoClassDefFoundError:PackageB.ForeignClass

非常感谢,

1 个答案:

答案 0 :(得分:0)

我认为你只注射PackageA,而foreignClass注入PackageB。您可以在web.xml中尝试此代码:

<init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>PackageA,PackageB</param-value>
    </init-param>

我希望这对你有所帮助。所有最好的,快乐的编码。