gpt4 book ai didi

java - jackson API的实现

转载 作者:行者123 更新时间:2023-12-01 12:16:18 25 4
gpt4 key购买 nike

我对jackson api完全陌生,我试图以json的形式从https://www.cyb3rpirat3s.in/android/leaderboard.php获取数据,但是在执行时,它使我陷入异常之下……请帮助并纠正我……。在此先感谢。

public class DataBindingFilter {
public static void main(String[] args) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
String url = "https://www.cyb3rpirat3s.in/android/leaderboard.php";
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
AlbumFilter info = mapper.readValue(new URL(url), AlbumFilter.class);
System.out.println(info.getLevel());
System.out.println(info.getPirateName());
System.out.println(info.getRank());
}

}


AlbumFilter类

@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.PUBLIC_ONLY)
public class AlbumFilter {
private String pirateName;
private String level;
private String rank;
protected String getPirateName() {
return pirateName;
}

public String getLevel() {
return level;
}
public String getRank(){
return rank;
}
}


Logcat错误

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1037)
at com.fasterxml.jackson.core.JsonFactory._optimizedStreamFromURL(JsonFactory.java:935)
at com.fasterxml.jackson.core.JsonFactory.createJsonParser(JsonFactory.java:527)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1782)
at jackson.DataBindingFilter.main(DataBindingFilter.java:17)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
... 16 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 22 more

最佳答案

正如大家在评论中解释的那样,问题与证书丢失有关。注释中的链接建议了一种如何安装证书的方法。还有另一种可行的测试方法。您可以使用以下实用程序类

public class SSLCertificate {
public static void disableSSLVerifier() throws NoSuchAlgorithmException, KeyManagementException {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

public static HttpClient getTestHttpClient(int port) {
try {

// Create all-trusting host name verifier
X509HostnameVerifier allHostsValid = new X509HostnameVerifier() {

public void verify(String host, SSLSocket ssl) throws IOException {
//Todo: missing implementation
}

public void verify(String host, X509Certificate cert) throws SSLException {
//Todo: missing implementation
}

public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
//Todo: missing implementation
}

public boolean verify(String s, SSLSession sslSession) {
return true; //Todo: missing implementation
}
};
org.apache.http.conn.ssl.SSLSocketFactory sf = new org.apache.http.conn.ssl.SSLSocketFactory(new TrustStrategy(){
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
}, allHostsValid);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", port, sf));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry);
return new DefaultHttpClient(ccm);
} catch (Exception e) {
return new DefaultHttpClient();
}
}

}


然后打电话

SSLCertificate.disableSSLVerifier(); 


在方法开始时

关于java - jackson API的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26988201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com