不同的servlet-api版本冲突

时间:2013-04-09 23:55:20

标签: rest maven jetty servlet-filters

我正在使用Maven和Jetty作为服务器开发RestEasy Web服务。其中一个服务应该使用扩展javax.servlet.http.HttpServletResponseWrapper的Filter。问题出在来自servlet-api 2.3的接口javax.servlet.ServletResponse的方法setCharacterEncoding中。我收到了以下异常

java.lang.NoSuchMethodError: com.webapp.rest.jsonp.HttpServletResponseContentWrapper.setCharacterEncoding(Ljava/lang/String;)V

我已经将servlet-api 2.4依赖于此,但也尝试了2.5:

 <web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Servlet-api 2.4的规定如下:

 <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
    <scope>provided</scope>
 </dependency>

我正在使用Maven jetty插件:

 <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.15</version>
<configuration>
 ....
 <exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>

My RestEasy配置:

 <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.2.2.GA</version>
<exclusions>
    <exclusion>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    </exclusion>

但是,在maven存储库中始终存在2.3和2.4(或2.5)版本。我无法弄清楚这个2.3版本是否带有其他依赖性。有什么建议我怎么能找到这个以及如何强制使用servlet-api 2.4。

我的依赖树看起来如下:

[INFO] [dependency:tree {execution: default-cli}]
[INFO] com.inextweb.ai:inextweb-ai:war:1.0.0-SNAPSHOT
[INFO] +- org.htmlparser:htmlparser:jar:2.1:compile
[INFO] +- org.htmlparser:htmllexer:jar:2.1:compile
[INFO] +- org.htmlparser:thumbelina:jar:2.1:compile
[INFO] +- org.htmlparser:sitecapturer:jar:2.1:compile
[INFO] +- org.htmlparser:filterbuilder:jar:2.1:compile
[INFO] +- net.sf.jung:jung-api:jar:2.0.1:compile
[INFO] |  \- net.sourceforge.collections:collections-generic:jar:4.01:compile
[INFO] +- net.sf.jung:jung-algorithms:jar:2.0.1:compile
[INFO] |  \- colt:colt:jar:1.2.0:compile
[INFO] |     \- concurrent:concurrent:jar:1.3.4:compile
[INFO] +- net.sf.jung:jung-graph-impl:jar:2.0.1:compile
[INFO] +- net.sf.jung:jung-visualization:jar:2.0.1:compile
[INFO] +- org.apache.opennlp:opennlp-tools:jar:1.5.2-incubating:compile
[INFO] |  +- org.apache.opennlp:opennlp-maxent:jar:3.0.2-incubating:compile
[INFO] |  \- jwnl:jwnl:jar:1.3.3:compile
[INFO] +- net.sf.jwordnet:jwnl:jar:1.4_rc3:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- org.testng:testng:jar:6.1.1:test
[INFO] |  +- org.beanshell:bsh:jar:2.0b4:test
[INFO] |  +- com.beust:jcommander:jar:1.12:test
[INFO] |  \- org.yaml:snakeyaml:jar:1.6:test
[INFO] +- commons-pool:commons-pool:jar:1.6:compile
[INFO] +- commons-codec:commons-codec:jar:1.7:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- log4j:log4j:jar:1.2.16:compile
[INFO] +- org.mongodb:mongo-java-driver:jar:2.10.1:compile
[INFO] +- org.jboss.resteasy:resteasy-jaxrs:jar:2.3.5.Final:compile
[INFO] |  +- org.jboss.resteasy:jaxrs-api:jar:2.3.5.Final:compile
[INFO] |  +- org.scannotation:scannotation:jar:1.0.3:compile
[INFO] |  |  \- javassist:javassist:jar:3.12.1.GA:compile
[INFO] |  +- javax.annotation:jsr250-api:jar:1.0:compile
[INFO] |  +- javax.activation:activation:jar:1.1:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.1.2:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.1.2:compile
[INFO] |  \- net.jcip:jcip-annotations:jar:1.0:compile
[INFO] +- org.jboss.resteasy:resteasy-jackson-provider:jar:2.2.2.GA:compile
[INFO] |  +- org.codehaus.jackson:jackson-core-asl:jar:1.6.3:compile
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.6.3:compile
[INFO] |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.6.3:compile
[INFO] |  \- org.codehaus.jackson:jackson-xc:jar:1.6.3:compile
[INFO] +- org.jboss.resteasy:resteasy-jettison-provider:jar:2.2.2.GA:compile
[INFO] |  +- org.jboss.resteasy:resteasy-jaxb-provider:jar:2.2.2.GA:compile
[INFO] |  |  \- com.sun.xml.stream:sjsxp:jar:1.0.1:compile
[INFO] |  \- org.codehaus.jettison:jettison:jar:1.2:compile
[INFO] +- org.simpleframework:simple-xml:jar:2.3.6:compile
[INFO] |  +- stax:stax-api:jar:1.0.1:compile
[INFO] |  \- stax:stax:jar:1.2.0:compile
[INFO] +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] +- com.google.code.gson:gson:jar:2.2.2:system
[INFO] +- junit:junit:jar:3.8.2:compile
[INFO] +- xalan:serializer:jar:2.7.1:compile
[INFO] +- bouncycastle:bcprov-jdk15:jar:140:compile
[INFO] +- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] +- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- xom:xom:jar:1.1:compile
[INFO] |  +- xerces:xmlParserAPIs:jar:2.6.2:compile
[INFO] |  +- xerces:xercesImpl:jar:2.6.2:compile
[INFO] |  +- xalan:xalan:jar:2.7.0:compile
[INFO] |  \- jaxen:jaxen:jar:1.1-beta-8:compile
[INFO] |     \- jdom:jdom:jar:1.0:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.6:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.6:compile
[INFO] +- org.slf4j:jul-to-slf4j:jar:1.6.6:compile
[INFO] \- org.slf4j:jcl-over-slf4j:jar:1.6.6:runtime
[INFO] ------------------------------------------------------------------------

这是整个pom.xmlweb.xml

由于

2 个答案:

答案 0 :(得分:11)

根据servlet api javadoc ...

自Servlet API 2.4以来,

javax.servlet.HttpSevletResponseWrapper.setCharacterEncoding(String)已经存在。

你有几件事需要照顾。

  1. WEB-INF/web.xml
  2. 中设置您的Servlet API版本
  3. 确保在项目中使用适合您要运行的Jetty版本的Servlet API。
    • Jetty 6 - Servlet API 2.4
    • Jetty 7 - Servlet API 2.5
    • Jetty 8 - Servlet API 3.0
    • Jetty 9 - Servlet API 3.1
  4. 从maven
  5. 获取相应的Servlet API jar文件
    <project>
      <build>
        <dependencies>
    
          <!-- Servlet API 2.4 -->
          <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
          </dependency>
    
          <!-- Servlet API 2.5 / Standard Location -->
          <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
          </dependency>
    
          <!-- Servlet API 2.5 / OSGi bundle ready -->
          <dependency>
            <groupId>org.eclipse.jetty.orbit</groupId>
            <artifactId>javax.servlet</artifactId>
            <version>2.5.0.v201103041518</version>
            <scope>provided</scope>
          </dependency>
    
          <!-- Servlet API 3.0 / Standard Location / OSGi bundle ready -->
          <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
          </dependency>
    
          <!-- Servlet API 3.1 / Standard Location / OSGi bundle ready -->
          <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
          </dependency>
    
        </dependencies>
      </build>
    </project>
    

答案 1 :(得分:0)

我发现这里有什么问题。在maven-jetty-plugin中,我添加了commons-logging的依赖项,每次运行jetty时都会调用servlet-api 2.3。一旦我删除了这种依赖,一切都很好。

谢谢你的帮助。