尝试在 Glassfish 上创建 WebClient 时出现 NoSuchMethodError

NoSuchMethodError when trying to create a WebClient on Glassfish

我正在尝试创建 HtmlUnit WebClient 以便在 Glassfish 服务器上的 Java EE Servlet filter 中解析 Ember 应用程序网页.

servlet代码:

  final WebClient webClient = new WebClient(); // causes error
  HtmlPage page = webClient.getPage(originalUrl);

pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.enunciate</groupId>
            <artifactId>maven-enunciate-plugin</artifactId>
            <version>1.29</version>
            <executions>
                <execution>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <configFile>enunciate.xml</configFile>
                <enunciateArtifactId/>
                <enunciateArtifactId/>
                <moduleName/>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceExcludes>**/web.xml</warSourceExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.15</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.15</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.enunciate</groupId>
        <artifactId>enunciate-rt</artifactId>
        <version>1.29</version>
    </dependency>

    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-core</artifactId>
        <version>4.0.6</version>
    </dependency>

    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-tcp</artifactId>
        <version>4.0.6</version>
    </dependency>

    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-extensions</artifactId>
        <version>4.0.6</version>
    </dependency>

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

    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>com.paymill</groupId>
        <artifactId>paymill-java</artifactId>
        <version>5.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.mindrot</groupId>
        <artifactId>jbcrypt</artifactId>
        <version>0.3m</version>
    </dependency>

    <dependency>
        <groupId>com.restfb</groupId>
        <artifactId>restfb</artifactId>
        <version>1.7.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.19.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-jackson</artifactId>
        <version>1.19.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-plus</artifactId>
        <version>v1-rev209-1.19.1</version>
    </dependency>

    <dependency>
        <groupId>org.codemonkey.simplejavamail</groupId>
        <artifactId>simple-java-mail</artifactId>
        <version>2.1</version>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.10</version>
    </dependency>
</dependencies>

错误:

java.lang.NoSuchMethodError: org.apache.http.impl.cookie.BrowserCompatSpecFactory.create(Lorg/apache/http/protocol/HttpContext;)Lorg/apache/http/cookie/CookieSpec;
    at com.gargoylesoftware.htmlunit.CookieManager.<init>(CookieManager.java:54)
    at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:136)
    at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:199)

显然将 HtmlUnit 依赖项移动到依赖项列表的底部解决了这个问题。就像提到的 peeskillet 一样,这不是一个完整的解决方案,因为我们仍然不知道首先导致问题的原因,但它可以作为第一个解决方案。如果找到罪魁祸首,请务必回答。

我有同样的错误,但 html 单元不是解决方案。我把 httpclient.jar 放在最上面,这样问题就解决了。