I have created a Java WebSocket Jetty server who uses an API to retrieve some data using HttpUrlConnection()
. I also have a MongoDb instance running in a docker container listening to port 27017. When running the server I create a connection with MongoDB and then try to retrieve some data with the API. Subsequently I get the following error:
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
Here's the weird part: without creating any connections to Mongo, the data retrieval runs smoothly with no errors or exceptions (the certificate exists). My guess is that maybe there is some conflict between the MongoDB driver and Http handler but I could not find anything online.
Here is the code for the API request:
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;
and here is the code to connect to the 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());
}
}
The exception is thrown in this line:
br = new BufferedReader(new InputStreamReader(con.getInputStream()));