更新时间:2023-01-18 20:30:17
根据您的描述,我还使用Document DB JavaSDK编写了一段代码连接到Cosmos DB仿真器,结果遇到了与您相同的问题。
package emulator;
import com.microsoft.azure.documentdb.ConnectionPolicy;
import com.microsoft.azure.documentdb.ConsistencyLevel;
import com.microsoft.azure.documentdb.Database;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
public class TestEmlulator {
// Replace with your DocumentDB end point and master key.
private static final String END_POINT = "https://localhost:8081/";
private static final String MASTER_KEY = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
public static void main(String[] args) throws DocumentClientException {
// Connect to the Azure Cosmos DB Emulator running locally
DocumentClient client = new DocumentClient(END_POINT, MASTER_KEY, ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
Database database = new Database();
database.setId("testEmulator");
database = client.createDatabase(database, null).getResource();
System.out.println(database.toJson());
}
}
所以我导出cosmosDB的证书,将其命名为documentdbemulatorcert.cer
,并按照official tutorial提供的方式存储在我的D盘中。您似乎也做了同样的事情。
步骤1:打开CMD命令窗口,进入%JAVA_HOME%/jre/lib/security
目录。请注意,必须使用administrator privileges
对我来说,目录看起来像C:Program FilesJavajdk1.8.0_131jrelibsecurity
步骤2:在打开的命令窗口中,输入以下命令:
keytool -import -alias cacerts -keystore cacerts -file d:documentdbemulatorcert.cer
输入默认密码:changeit
,然后在Trust this certificate? [no]:
第3步:您可以看到Certificate was added to keystore
证书是否已成功导入。
您可以参考下面的截图了解整个过程:
最后,我的代码可以工作了!
您也可以参考下面的SO线程:
1.Unable to find valid certification path to requested target - error even after cert imported
2."PKIX path building failed" and "unable to find valid certification path to requested target"
希望它能帮助您。