尝试使用API​​时出错.java.lang.NoSuchFieldError:INSTANCE

mah*_*der 9 java-7 apache-httpcomponents smartsheet-api

我正在尝试使用java程序连接到smartsheet api.最初我遇到了站点证书的问题,该证书通过将其添加到java密钥库来解决.现在,当我尝试运行我的代码时,我收到以下错误.

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
    at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
    at com.smartsheet.api.internal.http.DefaultHttpClient.<init>(DefaultHttpClient.java:64)
    at com.smartsheet.api.SmartsheetBuilder.build(SmartsheetBuilder.java:203)
    at SmartsheetConnection.main(SmartsheetConnection.java:13)
Run Code Online (Sandbox Code Playgroud)

这是我的代码(我按照他们的文档).

import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.models.enums.*;
import com.smartsheet.api.oauth.*;

public class SmartsheetConnection {
    public static void main(String[] args) throws SmartsheetException {
        // Set the access token.
        Token token = new Token();
        token.setAccessToken("foo");
        Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken(token.getAccessToken()).build();
    }
}
Run Code Online (Sandbox Code Playgroud)

投掷错误的行是(第144行)

@Deprecated
    public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER
        = AllowAllHostnameVerifier.INSTANCE; 
Run Code Online (Sandbox Code Playgroud)

但我不知道该怎么做.我正在使用maven来获取依赖项.它与Apache HttpComponents的版本有关吗?

这是pom.xml

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.smartsheet</groupId>
            <artifactId>smartsheet-sdk-java</artifactId>
            <version>2.0.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client</artifactId>
            <version>1.9</version>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)

Kim*_*ndl 9

关于此错误的其他帖子似乎表明它通常是由httpcorejar 的冲突版本引起的.即,httpcore类路径上的旧版本.

有关更多信息,我建议您查看以下帖子:


Ani*_*nha 5

我知道我回复得有点晚了,实际上我也在为同样的问题苦苦挣扎,我通过使用 Maven Shade 插件找到了解决方案。

问题是 JAR 冲突,可能您的项目使用的是不同版本的 HTTPclient,然后是您的 Appliaction 正在运行的容器。

要解决此问题,请使用下面的 Maven Shade 插件,它将 HttpClient 的包名称更改为打包 JAR 的指定名称。这也将重构代码中的所有用法。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <relocations>
              <relocation>
                <pattern>org.apache.http</pattern>
                <shadedPattern>org.shaded.apache.http</shadedPattern>
              </relocation>
            </relocations>
        </configuration>
    </execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

上面的示例将使用org.shaded.apache.httpfrom更改 HttpClient 包org.apache.http

Maven Shade 还将创建一个 fat/uber jar,因此您的最终包大小会增加,并且将包含您在 POM 中的依赖项中提到的所有类。

如果您不想在最终 jar 中包含所有依赖 jar,则将依赖的 Scope 添加为<scope>provided</scope>.