我正在Spring中进行一些测试,并在尝试构建简单的REST api时遇到问题。环境非常简单,但是我希望能够在Tomcat和Spring Boot中执行这两种部署方法。
在我的主项目中,我创建了这个类:
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;
}
}
我创建了一个application-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错误。
然后我尝试创建另一个项目,它将以前的项目封装为Spring启动应用程序
这个新项目的唯一内容如下
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>
我的春季启动应用程序的pom.xml
<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;博士
基本上: 如果我创建一个spring应用程序并在tomcat中运行,我会继续为我的REST调用获取404。
如果我将它封装在SpringBoot应用程序中,我会获得依赖项中的spring类的NoClassDefFoundError ...
答案 0 :(得分:0)
第一个应用程序没有弹簧配置,您可以通过将您的代码与spring.io上的示例进行比较来清楚地看到 对于第二个,我想这是因为spring-boot有一个不同版本的spring-core 在进行此类实验之前,您应该从the official guide开始
答案 1 :(得分:0)
出现此问题是因为您的maven项目的某些依赖项依赖于“spring-core”。早期版本4.1。为了发现它,我运行了以下maven命令:
c:\myapp>mvn dependency:tree
maven树(在这篇文章的最后)向我展示了依赖关系&org.springframework.shell:spring-shell:jar:1.1.0.RELEASE&#39;依赖于&#39; org.springframework:spring-core:jar:4.0.3.RELEASE&#39;这导致了这个问题(版本4.0.3早于4.1)。在我的情况下,解决方案是排除这种“依赖性”。在我的pom.xml中如下:
...
<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]
端。