玻璃鱼 5.0 java.lang.NoSuchMethodError

Glassfish 5.0 java.lang.NoSuchMethodError

当我尝试连接时 google 自定义搜索 api glassfish 抛出 nosuchmethoderror

这是代码

private int conn (String search)throws Exception {

    String key="mykey";
    URL url = new URL("https://www.googleapis.com/customsearch/v1?q="+search+"&cx=017576662512468239146%3Aomuauf_lfve&key="+key);
    doTrustToCertificates();
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setDoOutput(true);
    BufferedReader reader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
    String splitreader = "";
    StringBuilder ss = new StringBuilder();
    while((splitreader = reader.readLine()) != null){
        ss.append(splitreader);
}
    JSONObject json = new JSONObject(ss.toString());
    JSONObject jsonrequest = json.getJSONObject("queries").getJSONArray("request").getJSONObject(0);
    return jsonrequest.getInt("totalResults");
    }


 public void doTrustToCertificates() throws Exception {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                    return;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                    return;
                }
            }
    };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
                System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
            }
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

异常

java.lang.NoSuchMethodError: sun.security.ssl.SSLSessionImpl.(Lsun/security/ssl/ProtocolVersion;Lsun/security/ssl/CipherSuite;Ljava/util/Collection;Lsun/security/ssl/SessionId;Ljava/lang/String;I)V

但是代码适用于 glassfish 4.1.1 和 wildfly 我不明白为什么不能适用于 glassfish 5.0

检查您的 Java 版本。这是由于 JDK 围绕 JDK 8u161 的变化影响了 Grizzly。

总结一下:

  • 如果使用GlassFish 5.0,需要在JDK8以下JDK8u161
  • 的版本
  • 如果你使用GlassFish 5.0.1或更高版本,你需要在JDK8以上8u161
  • 版本

此错误已在 GlassFish GitHub 问题跟踪器中进行跟踪:
https://github.com/eclipse-ee4j/glassfish/issues/22436