Spring 3,Jersey(JSR-311)和Maven依赖

时间:2010-05-26 10:00:00

标签: java spring maven-2 jersey

我目前正在努力整合基于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)

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

谢谢!

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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 dependency:tree 
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Q2911869
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] com.stackoverflow:Q2911869:jar:1.0-SNAPSHOT
[INFO] +- com.sun.jersey.contribs:jersey-spring:jar:1.2:compile
[INFO] |  +- com.sun.jersey:jersey-server:jar:1.2:compile
[INFO] |  |  +- com.sun.jersey:jersey-core:jar:1.2:compile
[INFO] |  |  |  \- javax.ws.rs:jsr311-api:jar:1.1.1:compile
[INFO] |  |  \- asm:asm:jar:3.1:compile
[INFO] |  +- org.springframework:spring-core:jar:3.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-asm:jar:3.0.2.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:3.0.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-expression:jar:3.0.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:3.0.2.RELEASE:compile
[INFO] |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] \- junit:junit:jar:3.8.1:test
...

参考

答案 2 :(得分:3)

据我所知,他们尚未正式更换jersey-spring maven模块以支持弹簧3.0而不是2.5。我还在nabble forum发现了以下帖子!