标记jar时提供的java.lang.NoClassDefFoundError

时间:2014-11-24 09:17:55

标签: java maven tomcat war packaging

我在webapp中有以下依赖关系:

<!-- swagger -->
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>${version.swagger.springmvc}</version>
</dependency>

我想减小war文件的大小,所以我按照提供标记了swagger jar:

<!-- swagger -->
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>${version.swagger.springmvc}</version>
    <scope>provided</scope>
</dependency>

我将swagger jar添加到tomcat的常用lib文件夹中,因此它应该在运行时由tomcat提供,但是当我尝试deply app时,我得到了这个异常:

java.lang.IllegalStateException: Cannot load configuration class: com.mangofactory.swagger.configuration.DocumentationConfig
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:368)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:244)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:633)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1279)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1192)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getDeclaredMethods(Class.java:1962)
at org.springframework.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:348)
at org.springframework.cglib.proxy.Enhancer.getMethods(Enhancer.java:421)
at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:456)
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:118)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:92)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:358)
... 23 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
... 36 more

我注意到问题是来自org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses,但是我已经在我的pom中声明了所有需要的弹簧jar而没有提供的范围 在没有提供范围的情况下运行webapp它可以正常工作吗?

1 个答案:

答案 0 :(得分:0)

我认为问题来自另一个丢失的罐子。 在堆栈跟踪中,我们看到

Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver

com.fasterxml.classmate.TypeResolver存在于jar classmate.jar中,它是来自swagger-springmvc的依赖项。

使用范围编译时,也会部署swagger-springmvc的依赖关系。 在提供范围的情况下,您必须提供swagger-springmvc jar,以及它的所有依赖项。在这里,您需要提供classmate.jar。