Java Spring 3、Jersey (JSR-311) 和 Maven 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2911869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:24:15  来源:igfitidea点击:

Spring 3, Jersey (JSR-311) and Maven dependencies

javaspringmaven-2jersey

提问by fasseg

im currently struggling to integrate a REST Service based on Jersey and Spring. I'm using Spring 3.0.2-RELEASE and jersey-spring 1.2.

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

But jersey-spring adds a dependency to Spring 2.5.6 to my project which of cause conflicts with the 3.0.2-RELEASE to give me thefollwing error:

但是 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)

Is there a way to get around this issue? Does anyone know?

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

Thanks!

谢谢!

采纳答案by fasseg

Own Answer: I simply excluded the dependencies in the pom.xml in the following way. there don't seem to be any incompabilities and everything runs fine so far:

自己的回答:我只是通过以下方式排除了 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>

回答by Abdel Raoof

As far as I know they are yet to officially change the jersey-spring maven module to support spring 3.0 instead of 2.5. I also found the following post at nabble forum!

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

回答by Pascal Thivent

I don't know if jersey-spring is compatible with Spring 3.0 but the way to control the versions of artifacts used in transitive dependencies is to use dependencyManagement:

我不知道 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>

Note that it's still required to exclude org.springframework:spring:jar:2.5.6 since this dependency doesn't exist in later versions.

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

Below, the dependency tree you'll get with this setup:

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

$ 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
...

Reference

参考