不同的servlet-api版本冲突

zor*_*mic 4 rest jetty maven 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
Run Code Online (Sandbox Code Playgroud)

我已经将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">
Run Code Online (Sandbox Code Playgroud)

Servlet-api 2.4的指定方式如下:

 <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
    <scope>provided</scope>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

但是,在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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

这是整个pom.xmlweb.xml

谢谢

Joa*_*elt 11

根据servlet api javadoc ...

自Servlet API 2.4以来,javax.servlet.HttpSevletResponseWrapper.setCharacterEncoding(String)已经存在.

你有几件事需要照顾.

  1. 在您的.中设置您的Servlet API版本 WEB-INF/web.xml
  2. 确保在项目中使用适合您要运行的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
  3. 从maven中获取相应的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>
Run Code Online (Sandbox Code Playgroud)

  • 而且,你_usenot_使用Servlet API 2.3.您的初始错误消息和javadoc非常清楚.您_must_使用Servlet Api 2.4(或更新版本)来修复该特定错误消息. (2认同)