繁体   English   中英

出现错误java.lang.NoClassDefFoundError:在Jboss EAP 6.4.x中部署时,sun / net / www / protocol / https / HttpsURLConnectionImpl

[英]Getting Error java.lang.NoClassDefFoundError: sun/net/www/protocol/https/HttpsURLConnectionImpl when deployed in Jboss EAP 6.4.x

我正在使用java.net编写一个rest客户,它应该执行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

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

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

在尝试摆弄JDK的内部类之前,您是否尝试过使用X-HTTP-Method-Override解决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中使用的类不是您实现的替代方法,因此字段和方法的名称可能不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM