简体   繁体   中英

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

I know this issue has been posted here before, but I can't seem to find an answer to my question..

I'm trying to get Jersey from "ProjectA" to call a method in a POJO ("PojoClass"), which would instantiate an object from an imported class ("ForeignClass")and then call one of its own methods.. but I keep getting a NoClassDefFoundError error regarding ForeignClass. If I turn PojoClass to a Main, it finds ForeignClass with no problem and instantiates the object. is ForeignClass supposed to be imported into ProjectA's WEB-INF/lib somehow for Jersey to recognize it?

ProjectA's 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"

    }

}

And then http://localhost:8080/ProjectA/rest/admin produces: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: PackageB.ForeignClass

Thanks a lot,

I think you are inject only PackageA , but foreignClass in PackageB . You can try this code in web.xml :

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

I hope this is helpful for you. All the best, happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM