首页 > 解决方案 > 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?

标签: azurespring-bootazure-cosmosdb

解决方案


约翰我做了一些与你尝试的非常相似的事情,它对我有用,没有任何问题。

    @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


推荐阅读