Spring MVC - 为什么在部署上下文时出现 NoSuchMethodError 异常?

标签 spring tomcat deployment spring-mvc nosuchmethoderror

尽管这个项目已经为我工作了一段时间,但当我尝试在 Tomcat 中部署我的应用程序上下文时,我现在遇到了一个异常:

Servlet /testapp threw load() exception
java.lang.NoSuchMethodError: org.springframework.core.convert.converter.ConverterRegistry.addConverter(Ljava/lang/Class;Ljava/lang/Class;Lorg/springframework/core/convert/converter/Converter;)V
    at org.springframework.core.convert.support.DefaultConversionService.addScalarConverters(DefaultConversionService.java:62)

据我所知,如果应用程序中编译了多个版本的 Spring,就会发生这种情况。但是,我已经完全清除了我的本地依赖项存储库,以确认我的构建路径中只有我想要的版本。我的依赖如下:

<properties>
    <java-version>1.6</java-version>
    <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencies>
    <!-- CGLIB, only required and used for @Configuration usage -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>2.2.2</version>
    </dependency>
    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>

我在 Eclipse 项目中没有编译器错误,它构建完美,没有警告或错误。但是,Tomcat 将我的单个测试 servlet 标记为不可用,当我跟踪日志时,上面的堆栈跟踪是我唯一的提示。

任何人都可以提供任何帮助,我们将不胜感激。我对 Spring 和 Spring MVC 的经验很少,所以我希望这只是一个我完全没有遇到的新手问题。

最佳答案

感谢@nickdos 提供的提示。最初给我带来麻烦的 pom.xml 更改已被删除,清除我的工作目录使我恢复了生机。但是,我最初的问题是由于将 Jersey jar 包含到我的项目中而引入的冲突。

我的项目正在使用 Spring v3.x,但 Jersey jar 包含对 Spring 2.5 的依赖,导致冲突。为了解决这个问题,我声明了一些排除项如下:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${com.sun.jersey.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${com.sun.jersey.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${com.sun.jersey.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>${com.sun.jersey.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </exclusion>
    </exclusions>
</dependency>

@nickdos 的评论使我的项目在不包含 Jersey 的情况下恢复了速度,并且通过声明排除项,我现在支持 Spring MVC 以及 Jersey REST 支持,而没有我报告的 load() 异常之前。

谢谢大家!

关于Spring MVC - 为什么在部署上下文时出现 NoSuchMethodError 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9754857/

相关文章:

asp.net-mvc-4 - 为什么我的 Azure 部署显示我的空 MVC 4 应用程序对于超小应用程序来说太大了?

git - 如何提供对 Composer 使用的 git 存储库的访问

spring - 从 web.xml 初始化一个 spring bean

java - 行业标准 Java(Java EE、Java SE 或 Spring)应用程序源代码示例

java - org.apache.catalina.LifecycleException : Failed to start component [StandardEngine[Catalina]. StandardHost[localhost].StandardContext[/CollegeWebsite]]

java - 如何在 OpenShift 上启动与 JDBC 的连接?

java - servlet 容器是否可以防止 web 应用程序相互干扰,它们是如何做到的?

java - 如何在 Spring 中使用注释验证并从属性文件中获取错误消息?

Spring 数据 mongodb @DBRef 列表

django - 使用 Docker 部署 Django 应用