【发布时间】:2017-11-05 01:38:32
【问题描述】:
我创建了一个 Java WebSocket Jetty 服务器,它使用 API 使用 HttpUrlConnection() 检索一些数据。我还有一个在 docker 容器中运行的 MongoDb 实例,监听端口 27017。运行服务器时,我创建了与 MongoDB 的连接,然后尝试使用 API 检索一些数据。随后我收到以下错误:
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
这是奇怪的部分:无需创建与 Mongo 的任何连接,数据检索运行顺利,没有错误或异常(证书存在)。我的猜测是MongoDB驱动程序和Http处理程序之间可能存在一些冲突,但我在网上找不到任何东西。
这是 API 请求的代码:
URL url=new URL(s);
HttpURLConnection con =(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
br = new BufferedReader(
new InputStreamReader(con.getInputStream()));
while ((line = br.readLine()) != null) {
jsonData += line + "\n";
}
JSONObject ob;
这是连接到 MongoDB 的代码:
public void connect() {
try {
client=new MongoClient("localhost:27017");
db=client.getDatabase("QuoteDB");
coll = db.getCollection("quotes");
}catch(MongoException e) {
System.out.println(e.getMessage());
}
}
在这一行抛出异常:
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
【问题讨论】:
标签: java mongodb exception httpurlconnection x509certificate