azure - Azure DocumentDB 和 Spring Boot 冲突
问题描述
在我的 Spring Boot 应用程序中,我client.getDatabaseAccount()
在两个地方运行这个简单的代码:
1) 在main
方法中。在弹簧靴“启动”之前,DocumentClient
效果很好!!!
public static void main(String[] args) {
// just to test if it can connect to cosmos db before spring boot starts
DocumentClient client = new DocumentClient("URL","KEY", new ConnectionPolicy(), ConsistencyLevel.Session);
// this runs great and connects to azure cosmos db.
client.getDatabaseAccount();
SpringApplication.run(DemoApplication.class, args);
}
2)在服务类中。当调用以下方法(具有完全相同的代码client.getDatabaseAccount()
)时,它会引发异常
@Service
@Component
public class TestService {
public DocumentClient connectCosmos() throws Exception {
DocumentClient client = new DocumentClient("URL","KEY", new ConnectionPolicy(), ConsistencyLevel.Session);
// this throws an exception
client.getDatabaseAccount();
}
}
错误:
Execution encountered exception: 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, status code 403 sub status code null.
显然,证书没有问题,因为两段代码在同一个 SpringBootApplication 中运行
这是POM azure-documentdb
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
为什么会这样?我唯一的猜测是 Spring Boot 不喜欢 azure-documentdb。如何azure-documentdb
在 Spring Boot 中执行代码。我不想使用Azure Document DB Spring Boot Starter
,因为我的 spring boot 后端数据库是别的东西,我只需要 azure-documentdb
检索少量数据。我不想Azure Document DB Spring Boot Starter
将整个 Spring Boot 项目备份为 ORM
. 那么是否可以azure-documentdb
在 Spring Boot 项目中使用 pom?
解决方案
约翰我做了一些与你尝试的非常相似的事情,它对我有用,没有任何问题。
@Service
public class CosmosService {
public void connectCosmos() throws Exception {
DocumentClient client = new DocumentClient("https://somename-cosmosdb.documents.azure.com:443/", "somepassword", new ConnectionPolicy(), ConsistencyLevel.Session);
client.getDatabaseAccount();
FeedOptions options = new FeedOptions();
options.setEnableCrossPartitionQuery(true);
List<Document> result = client
.queryDocuments(
"dbs/" + "samples" + "/colls/" + "orders",
"SELECT * FROM c",
options)
.getQueryIterable()
.toList();
result.stream().forEach(System.out::println);
}
}
应用类
@SpringBootApplication
public class RatellaStackApplication implements CommandLineRunner {
@Autowired
CosmosService cosmosService;
public static void main(String[] args) {
SpringApplication.run(RatellaStackApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
cosmosService.connectCosmos();
System.exit(0);
}
}
依赖
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.7</version>
</dependency>
从控制器调用服务方法也可以。
@RestController
public class MyController {
@Autowired
CosmosService cosmosService;
@GetMapping("/hello")
public String hello() throws Exception {
cosmosService.connectCosmos();
return "OK";
}
}
链接到 Rest API 的源代码。为简单起见,我将所有内容都放在 SpringBoot Application 类中。 https://gist.github.com/RaviTella/e544ef7b266ba425abead7f05193f717
推荐阅读
- rest - 待办事项的 TFS 2018 REST API 调用 // API 版本 4.1
- java - 用于自定义 NULL 值序列化的 Jackson 自定义注释
- java - 如何在 AWS 上运行带有自动回归测试套件的分析器
- javascript - 测试 Solidity 合约时如何从 Javascript 初始化结构?
- angular - ES6/TypeScript 导入和 Angular
- php - php反序列化不起作用
- audiokit - AKAppleSampler 更改声音字体预设
- c - 将 Struct 数组作为指针传递
- javascript - 测试下载的 PDF 文件中的内容
- react-native-firebase - react-native-firebase scheduleNotification