java.lang.ClassNotFoundException: javax.net.ssl.SSLSocketFactory 在 WildFly8.2.0 上使用 rest easy with JAVA 8

java.lang.ClassNotFoundException: javax.net.ssl.SSLSocketFactory When using rest easy with JAVA 8 on WildFly8.2.0

我正在使用 rest easy 从第三方获取和发布数据 URL,当我使用以下代码调用获取服务时,我收到 ClassNotFoundException "javax.net.ssl.SSLSocketFactory"

final ClientRequest request = createRequest(url,acceptType,consumesType,body);
final byte[] encodedCredentials = (userName + ":" + password)
            .getBytes();
    final String encodedAuto = Base64.encodeBytes(encodedCredentials);
    request.header("Authorization", "Basic " + encodedAuto);
try {
        request.addAuthenticationHeaders(request, userName,
                password);
        response = request.get(String.class);
        if (response != null) {
            logger.info("Status of the REST Call:"
                    + response.getStatus());
        }
    } catch (final Exception e) {
        e.printStackTrace();
        logger.error("Failed the get the data from PM", e);
    }

createRequest方法如下

public ClientRequest createRequest(final String urlString,
        final String acceptType, final String consumesType,
        final String body) {
    final ClientRequest request = new ClientRequest(urlString);
    request.accept(acceptType);
    if (body != null) {
        request.body(consumesType, body);
    }
    request.header("Content-Type", consumesType);
    // request.header("Accept", acceptType);

    return request;
}

当它正在执行时 response = request.get(request);它抛出 SSLSocketFactory ClassNotFoundException,根据我的分析,这个 class 在 rt.jar 上可用,不需要添加到 WildFly 服务器或部署 lib 文件夹。

Caused by: java.lang.NoClassDefFoundError: javax/net/ssl/SSLSocketFactory
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:82) [commons-httpclient-3.1.jar:]
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:127) [commons-httpclient-3.1.jar:]
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707) [commons-httpclient-3.1.jar:]
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387) [commons-httpclient-3.1.jar:]
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) [commons-httpclient-3.1.jar:]
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) [commons-httpclient-3.1.jar:]
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323) [commons-httpclient-3.1.jar:]
at org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor.execute(ApacheHttpClientExecutor.java:81) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:39) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.security.doseta.DigitalSigningInterceptor.execute(DigitalSigningInterceptor.java:107) [resteasy-crypto-3.0.10.Final.jar:]
at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPInterceptor.execute(AcceptEncodingGZIPInterceptor.java:40) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:473) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:704) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.client.ClientRequest.get(ClientRequest.java:509) [resteasy-jaxrs-2.2.1.GA.jar:]
at org.jboss.resteasy.client.ClientRequest.get(ClientRequest.java:537) [resteasy-jaxrs-2.2.1.GA.jar:]
at com.example.main.framework.service.RestClientService.get(RestClientService.java:46) [main-ejb.jar:]
at com.example.main.entity.validator.EntityValidator.getPMAccountDetails(EntityValidator.java:2866) [main-ejb.jar:]
at com.example.main.entity.validator.EntityValidator.validateCompanySettingPortfolioMgr(EntityValidator.java:2633) [main-ejb.jar:]
at com.example.main.action.codegen.CompanySettingPortfolioMgrHome.persist(CompanySettingPortfolioMgrHome.java:329) [main-ejb.jar:]
... 99 more
Caused by: java.lang.ClassNotFoundException: javax.net.ssl.SSLSocketFactory from [Module "org.apache.commons.httpclient:main" from local module loader @6043cd28 (finder: local module finder @cb51256 (roots: D:\wildfly-8.2.0.Final\modules,D:\wildfly-8.2.0.Final\modules\system\layers\base))]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:213) [jboss-modules.jar:1.3.3.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459) [jboss-modules.jar:1.3.3.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408) [jboss-modules.jar:1.3.3.Final]
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389) [jboss-modules.jar:1.3.3.Final]
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134) [jboss-modules.jar:1.3.3.Final]
... 120 more

如果我们将 rt.jar 添加到 wildfly 模块,那么它会在 jsse.jar 中抛出与 classes 相关的 ClassNotFoundException,它是 JDK 1.8 库的一部分。然后我意识到我们不应该将这些 jar 添加到 httpclient 模块和部署 lib 文件夹中。

如果有人对此问题有解决方案,请告诉我。

将 "javax.api" 依赖项添加到模块 org.apache.commons.httpclient 的 module.xml 后问题得到解决。

<module ...>

    <resources>
        ...
    </resources>

    <dependencies>
        ....
        <module name="javax.api"/> 
    </dependencies>
</module>