2014-09-29 101 views
1

我正在Spring中进行一些测试,并在尝试构建简单的REST API时遇到问题。这个环境非常简单,但我希望能够在Tomcat和Spring Boot中执行两种部署方法。未找到春天宁静的服务。 NoClassDefFound与SpringBoot集成时

在我的主要项目中,我创建了这个类:

package springTest.tomcat; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class SpringTest { 

    @RequestMapping(value="/greeting",method=RequestMethod.GET) 
    public String greeting(@RequestParam(value="name",required=true) String name){ 
     return "Hello "+name; 
    } 
} 

我创建了一个应用程序的context.xml具有以下内容:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="controller" class="springTest.tomcat.SpringTest" /> 
</beans> 

我的应用程序有DynamicWebProject小,所以我出口springTest.war到tomcat 8并启动它,运行在9090端口上。

然后导航到localhost:9090/springTest/greeting?name = Aaron,但是我得到了404错误河

然后我试图创建另一个项目,当我尝试执行此这将封装以前的项目作为春季启动应用程序

这个新项目的唯一内容如下

package springTest.Boot; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class SpringTest { 
    public static void main(String[] args){ 
     SpringApplication.run(SpringTest.class, args); 
    } 

    @Bean 
    public springTest.tomcat.SpringTest springTest(){ 
     return new springTest.tomcat.SpringTest(); 
    } 
} 

但是我得到以下堆栈跟踪:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/OrderComparator$OrderSourceProvider 
    at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:101) 
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:60) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at java.lang.Class.newInstance(Unknown Source) 
    at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:78) 
    at org.springframework.boot.SpringApplication.createApplicationContext(SpringApplication.java:528) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:292) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) 
    at springTest.Boot.SpringTest.main(SpringTest.java:14) 
Caused by: java.lang.ClassNotFoundException: org.springframework.core.OrderComparator$OrderSourceProvider 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 13 more 

这里是我的核心应用

的pom.xml
<properties> 
    <spring.core.version>4.0.6.RELEASE</spring.core.version> 
    <spring.web.version>4.1.0.RELEASE</spring.web.version> 
</properties> 
<build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source /> 
       <target /> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.core.version}</version> 
    </dependency> 
     <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.web.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.web.version}</version> 
    </dependency> 
</dependencies> 

而对于我的春天启动应用程序

<properties> 
    <spring.boot.version>1.1.4.RELEASE</spring.boot.version> 
</properties> 
<build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <version>1.1.7.RELEASE</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>repackage</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source /> 
       <target /> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-amqp</artifactId> 
     <version>${spring.boot.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>SpringTest_Tomcat</groupId> 
     <artifactId>SpringTest_Tomcat</artifactId> 
     <version>0.0.1-SNAPSHOT</version> 
    </dependency> 
</dependencies> 

TL pom.xml中;博士

基本上是: 如果我让Spring应用程序,并从Tomcat中运行我不断收到404的为我的REST通话。

如果我在SpringBoot applicaction封装它,我得到一个弹簧类是存在的依赖关系的NoClassDefFoundError的...包含代码

回购: https://github.com/mangusbrother/SpringTests

+0

你明显处于依赖洞中,只是试图让自己离开那里。 – Jaiwo99 2014-09-29 15:27:59

+0

减少了依赖关系,只有需要。仍然遇到同样的问题。 – Maxwell 2014-09-29 15:48:54

+0

呵呵。我不相信你 – Jaiwo99 2014-09-29 15:51:58

回答

0

第一个应用程序不有弹簧配置,你可以通过比较你的代码和spring.io上的例子清楚地看到它。
第二,我想这是因为春季引导有不同版本的弹簧核心。
你应该从以下the official guide开始,做

+0

第一个应用程序具有应用程序 - context.xml,第二个不加载spring core本身。 – Maxwell 2014-09-29 16:41:26

+0

您在第一个应用中没有专用的web.xml或web mvc配置,因此无法在容器中启动它。第二个依赖于SprintTest_Tomcat应用程序,该应用程序具有弹簧核心依赖性。现在你知道它不会加载spring core_,难怪在Spring-core – hsluo 2014-09-30 02:15:13

+0

中有一个ClassNotFoundException,NoClassDef错误是通过将SpringTest_Tomcat应用程序spring.core版本更新为4.1.0来解决的。即使没有web.xml或web mvc配置,SpringTest_Boot应用程序仍然可以正常工作,尽管Tomcat应用程序并不依赖于它自己。 – Maxwell 2014-09-30 05:18:59

0

发生此问题,因为你的Maven项目的一些相关性取决于一个“弹簧核心”早期版本4.1的这样一个实验前。要发现它,我跑以下Maven命令:

c:\myapp>mvn dependency:tree 

一个Maven树(在最后的这个职位的)让我发现,依赖“org.springframework.shell:spring-shell:jar:1.1.0 .RELEASE'对于导致此问题的'org.springframework:spring-core:jar:4.0.3.RELEASE'具有依赖关系(版本4.0.3早于4.1)。就我而言,该解决方案是在我的pom.xml排除这种“子dependecy”如下:

... 
     <dependency> 
      <groupId>org.springframework.shell</groupId> 
      <artifactId>spring-shell</artifactId> 
      <version>1.1.0.RELEASE</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</artifactId> 
       </exclusion> 
       <exclusion> 
        <artifactId>org.springframework</artifactId> 
        <groupId>spring-context-support</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
.... 

这是我Maven的依赖关系树输出:

[INFO] ------------------------------------------------------------------------ 
[INFO] Building console 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ console --- 
[INFO] br.com.mycompany.myapp:console:jar:1.0-SNAPSHOT 
[INFO] +- br.com.mycompany.myapp:core:jar:1.0-SNAPSHOT:compile 
[INFO] | +- org.springframework:spring-tx:jar:4.1.6.RELEASE:compile 
[INFO] | +- org.hibernate:hibernate-validator:jar:4.3.1.Final:compile 
[INFO] | | \- org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile 
[INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile 
[INFO] | +- javax.validation:validation-api:jar:1.0.0.GA:compile 
[INFO] | \- org.hibernate:hibernate-jpamodelgen:jar:4.3.3.Final:compile 
[INFO] |  \- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile 
[INFO] +- br.com.mycompany.myapp:hibernateRepo:jar:1.0-SNAPSHOT:compile 
[INFO] | +- org.hibernate:hibernate-core:jar:4.3.3.Final:compile 
[INFO] | | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile 
[INFO] | | +- dom4j:dom4j:jar:1.6.1:compile 
[INFO] | | | \- xml-apis:xml-apis:jar:1.0.b2:compile 
[INFO] | | +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.4.Final:compile 
[INFO] | | +- org.javassist:javassist:jar:3.18.1-GA:compile 
[INFO] | | +- antlr:antlr:jar:2.7.7:compile 
[INFO] | | \- org.jboss:jandex:jar:1.1.0.Final:compile 
[INFO] | +- org.hibernate:hibernate-entitymanager:jar:4.3.3.Final:runtime 
[INFO] | \- org.springframework:spring-orm:jar:4.1.6.RELEASE:compile 
[INFO] |  \- org.springframework:spring-jdbc:jar:4.1.6.RELEASE:compile 
[INFO] +- br.com.mycompany.myapp:hsqldb-repo:jar:1.0-SNAPSHOT:compile 
[INFO] | +- org.hsqldb:hsqldb:jar:2.3.3:compile 
[INFO] | \- org.liquibase:liquibase-core:jar:2.0.5:compile 
[INFO] +- org.springframework.shell:spring-shell:jar:1.1.0.RELEASE:compile 
[INFO] | +- commons-io:commons-io:jar:2.3:compile 
[INFO] | +- jline:jline:jar:2.11:compile 
[INFO] | +- com.google.guava:guava:jar:15.0:compile 
[INFO] | +- cglib:cglib:jar:2.2.2:compile 
[INFO] | | \- asm:asm:jar:3.3.1:compile 
[INFO] | +- org.springframework:spring-context-support:jar:4.0.3.RELEASE:compile 
[INFO] | \- org.springframework:spring-core:jar:4.0.3.RELEASE:compile 
[INFO] |  \- commons-logging:commons-logging:jar:1.1.3:compile 
[INFO] +- org.fusesource.jansi:jansi:jar:1.11:runtime 
[INFO] +- org.springframework:spring-context:jar:4.1.6.RELEASE:compile 
[INFO] | +- org.springframework:spring-aop:jar:4.1.6.RELEASE:compile 
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile 
[INFO] | +- org.springframework:spring-beans:jar:4.1.6.RELEASE:compile 
[INFO] | \- org.springframework:spring-expression:jar:4.1.6.RELEASE:compile 
[INFO] +- org.springframework:spring-test:jar:4.1.6.RELEASE:test 
[INFO] +- junit:junit:jar:4.11:test 
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test 
[INFO] \- joda-time:joda-time:jar:2.9:compile 
[INFO] 

结束。