【问题标题】:Getting Error java.lang.NoClassDefFoundError: sun/net/www/protocol/https/HttpsURLConnectionImpl when deployed in Jboss EAP 6.4.x在 Jboss EAP 6.4.x 中部署时出现错误 java.lang.NoClassDefFoundError: sun/net/www/protocol/https/HttpsURLConnectionImpl
【发布时间】:2017-11-06 11:21:25
【问题描述】:

我正在使用 java.net 编写一个应该执行 PATCH 请求的休息客户端。但是由于 PATCH 在 java.net 中不是受支持的方法,所以我使用反射来通过更改代码来支持它,例如

private void updateConnectionToSupportPatchRequest(final HttpURLConnection conn)
    throws ReflectiveOperationException {
    try {
        final Object targetConn;
        if (conn instanceof HttpsURLConnectionImpl) {
            final Field delegateField = HttpsURLConnectionImpl.class.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            targetConn = delegateField.get(conn);
        } else {
            targetConn = conn;
        }
        final Field methodField = HttpURLConnection.class.getDeclaredField("method");
        methodField.setAccessible(true);
        methodField.set(targetConn, "PATCH");
    } catch (final NoSuchFieldException ex) {
        LOGGER.error("NoSuchFieldException: {} ", ex.getMessage());
    }
}

但是当我在 JBoss 中部署使用我的 rest 客户端的应用程序时,我收到了这个错误 -

java.lang.NoClassDefFoundError: sun/net/www/protocol/https/HttpsURLConnectionImpl

我查看了这个错误并发现了这个帖子http://planet.jboss.org/post/dealing_with_sun_jdk_related_noclassdeffounderror_under_jboss

我尝试了帖子中建议的解决方案,但仍然遇到相同的错误。有关如何通过此问题的任何想法?

附:我不能使用 Apache HttpClient 或 RestEasy(Jboss),因为项目中使用了另一个 3PP,它不支持 Apache HttpClient

【问题讨论】:

    标签: java jboss http-patch


    【解决方案1】:

    在尝试摆弄 JDK 的内部类之前,您是否尝试过 using the workaround X-HTTP-Method-Override?如果是这种情况,您可以使用实例的getClass-方法来访问字段并使用isAssignableFrom 替代instanceof

    另一种摆脱指定具体类的方法是尝试获取HttpsURLConnection 中的字段,并在找不到该字段时假设一个非Https-URLConnection。这可能类似于以下代码:

    private void updateConnectionToSupportPatchRequest(final HttpURLConnection conn) 
        throws ReflectiveOperationException {
        try {
            final Object targetConn = conn;
            try {
                final Field delegateField = findField(conn.getClass(), "delegate");
                delegateField.setAccessible(true);
                targetConn = delegateField.get(conn);
            }
            catch(NoSuchFieldException nsfe) {
                // no HttpsURLConnection
            }
            final Field methodField = findField(conn.getClass(), "method");
            methodField.setAccessible(true);
            methodField.set(targetConn, "PATCH");
        } catch (final NoSuchFieldException ex) {
            LOGGER.error("NoSuchFieldException: {} ", ex.getMessage());
        }
    }
    
    private Field findField(Class clazz, String name) throws NoSuchFieldException {
        while (clazz != null) {
            try {
                return clazz.getDeclaredField(name);
            }
            catch(NoSuchFieldException nsfe) {
                // ignore
            }
            clazz = clazz.getSuperclass();
        }
        throw new NoSuchFieldException(name);
    }
    

    但这可能会在另一个层面上失败,因为 - 显然 - JBoss 中使用的类不是您实现解决方法的类,因此字段和方法可能会以不同的方式命名。

    【讨论】:

    • 是的,我已经尝试过“X-HTTP-Method-Override”,但仍然在下面,它发送一个 POST 并且我返回一个 405 错误。我尝试了使用 isAssignableFrom 的建议,但仍然出现相同的错误:- java.lang.NoClassDefFoundError: sun/net/www/protocol/https/HttpsURLConnectionImpl
    猜你喜欢
    • 1970-01-01
    • 2016-05-15
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多