在我的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>
从控制器调用service方法也可以。
@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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句