Different servlet-api versions collision

2019-06-26 04:11发布

问题:

I'm developing RestEasy web services using Maven and Jetty as the server. One of the services should use Filter that extends javax.servlet.http.HttpServletResponseWrapper. The problem is in method setCharacterEncoding of the interface javax.servlet.ServletResponse which comes from servlet-api 2.3. I'm getting the following exception

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

I've speficied servlet-api 2.4 dependency as this, but also tried with 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 is specified like this:

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

I'm using Maven jetty plugin:

 <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 configuration:

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

However, in maven repository there are always both 2.3 and 2.4 (or 2.5) versions. I can't figure out if this 2.3 version comes with some other dependency. Is there any suggestion how could I find out this and how to enforce servlet-api 2.4 to be used.

My dependency tree looks is as follow:

[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] ------------------------------------------------------------------------

and here is the whole pom.xml and web.xml

Thanks

回答1:

According to the servlet api javadoc...

javax.servlet.HttpSevletResponseWrapper.setCharacterEncoding(String) has existed since Servlet API 2.4.

You have a few things to take care of.

  1. Set your Servlet API version in your WEB-INF/web.xml
  2. Ensure you use the appropriate Servlet API in your project for the version of Jetty you intend on running on.
    • Jetty 6 - Servlet API 2.4
    • Jetty 7 - Servlet API 2.5
    • Jetty 8 - Servlet API 3.0
    • Jetty 9 - Servlet API 3.1
  3. Grab the appropriate Servlet API jar file from maven
<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>


回答2:

I found what is the problem here. In maven-jetty-plugin I added dependency for commons-logging which pulled servlet-api 2.3 each time I run jetty. Once I removed this dependency, everything was fine.

Thank you for you help.