10

我目前正在努力集成基于 Jersey 和 Spring 的 REST 服务。我正在使用 Spring 3.0.2-RELEASE 和 jersey-spring 1.2。

但是 jersey-spring 在我的项目中添加了对 Spring 2.5.6 的依赖,这会导致与 3.0.2-RELEASE 冲突,从而给我以下错误:

11:58:25,409 ERROR org.springframework.web.context.ContextLoader:215 - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [cloverjazz-web-context.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getLocalName(Lorg/w3c/dom/Node;)Ljava/lang/String;
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)

有没有办法解决这个问题?有人知道吗?

谢谢!

4

3 回答 3

18

自己的答案:我只是通过以下方式排除了 pom.xml 中的依赖项。似乎没有任何不兼容之处,到目前为止一切正常:

 <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.2</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>
于 2010-05-26T13:15:42.170 回答
11

我不知道 jersey-spring 是否与 Spring 3.0 兼容,但控制传递依赖项中使用的工件版本的方法是使用dependencyManagement

<project>
  ...
  <repositories>
    <repository>
      <id>download-java-net</id>
      <url>http://download.java.net/maven/2/</url>
    </repository>
  </repositories>
  <properties>
    <org.springframework.version>3.0.2.RELEASE</org.springframework.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>${org.springframework.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.sun.jersey.contribs</groupId>
      <artifactId>jersey-spring</artifactId>
      <version>1.2</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    ...
  </dependencies>
</project>

请注意,仍然需要排除 org.springframework:spring:jar:2.5.6 因为此依赖项在以后的版本中不存在。

下面,您将通过此设置获得依赖关系树:

$ mvn 依赖:树
[INFO] 正在扫描项目...
[INFO] 搜索带有前缀的插件的存储库:“依赖”。
[信息] --------------------------------------------- -------------------------
[INFO] 楼 Q2911869
[INFO] 任务段:[依赖:树]
[信息] --------------------------------------------- -------------------------
[信息] [依赖:树{执行:默认-cli}]
[信息] com.stackoverflow:Q2911869:jar:1.0-SNAPSHOT
[信息] +- com.sun.jersey.contribs:jersey-spring:jar:1.2:compile
[信息] | +- com.sun.jersey:jersey-server:jar:1.2:compile
[信息] | | +- com.sun.jersey:jersey-core:jar:1.2:compile
[信息] | | | \- javax.ws.rs:jsr311-api:jar:1.1.1:compile
[信息] | | \- asm:asm:jar:3.1:编译
[信息] | +- org.springframework:spring-core:jar:3.0.2.RELEASE:compile
[信息] | | +- org.springframework:spring-asm:jar:3.0.2.RELEASE:compile
[信息] | | \- commons-logging:commons-logging:jar:1.1.1:compile
[信息] | +- org.springframework:spring-beans:jar:3.0.2.RELEASE:compile
[信息] | +- org.springframework:spring-context:jar:3.0.2.RELEASE:compile
[信息] | | +- org.springframework:spring-aop:jar:3.0.2.RELEASE:compile
[信息] | | \- org.springframework:spring-expression:jar:3.0.2.RELEASE:compile
[信息] | \- org.springframework:spring-web:jar:3.0.2.RELEASE:compile
[信息] | \- aopalliance:aopalliance:jar:1.0:compile
[信息] \- junit:junit:jar:3.8.1:test
...

参考

于 2010-05-26T13:56:33.760 回答
3

据我所知,他们尚未正式更改 jersey-spring maven 模块以支持 spring 3.0 而不是 2.5。我还在 nabble论坛上找到了以下帖子!

于 2010-05-26T10:09:09.250 回答