1

我试图使用 cosmos DB Document API 制作一个简单的 Spring Boot 应用程序(Table API 也是一个选项)

遵循示例并使用本地模拟器

Spring Boot 2.3 Azure Spring Boot:2.3.1

应用程序无法通过以下方式引导:

    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    ... 71 common frames omitted
Caused by: java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 88
    at java.util.Base64$Decoder.decode0(Base64.java:742) ~[na:1.8.0_241]
    at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_241]
    at com.azure.data.cosmos.internal.BaseAuthorizationTokenProvider.getMacInstance(BaseAuthorizationTokenProvider.java:251) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.internal.BaseAuthorizationTokenProvider.<init>(BaseAuthorizationTokenProvider.java:36) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.internal.RxDocumentClientImpl.<init>(RxDocumentClientImpl.java:200) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.internal.RxDocumentClientImpl.<init>(RxDocumentClientImpl.java:135) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.internal.RxDocumentClientImpl.<init>(RxDocumentClientImpl.java:124) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.internal.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:177) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.CosmosClient.<init>(CosmosClient.java:54) ~[azure-cosmos-3.7.3.jar:na]
    at com.azure.data.cosmos.CosmosClientBuilder.build(CosmosClientBuilder.java:203) ~[azure-cosmos-3.7.3.jar:na]

这里是一个用于重现错误的存储库,位于主要配置文件下方:

@Slf4j
@Configuration
@EnableCosmosRepositories
public class CosmosConfiguration extends AbstractCosmosConfiguration {

    @Value("${azure.cosmosdb.uri}")
    private String uri;

    @Value("${azure.cosmosdb.key}")
    private String key;

    @Value("${azure.cosmosdb.secondaryKey}")
    private String secondaryKey;

    @Value("${azure.cosmosdb.database}")
    private String dbName;

    @Value("${azure.cosmosdb.populateQueryMetrics}")
    private boolean populateQueryMetrics;

    private CosmosKeyCredential cosmosKeyCredential;

    private static class ResponseDiagnosticsProcessorImplementation implements ResponseDiagnosticsProcessor {

        @Override
        public void processResponseDiagnostics(@Nullable ResponseDiagnostics responseDiagnostics) {
            log.info("Response Diagnostics {}", responseDiagnostics);
        }
    }

    @Bean
    @Primary
    public CosmosDBConfig getConfig() {
        this.cosmosKeyCredential = new CosmosKeyCredential(key);
        CosmosDBConfig cosmosdbConfig = CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
        cosmosdbConfig.setPopulateQueryMetrics(populateQueryMetrics);
        cosmosdbConfig.setResponseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation());
        return cosmosdbConfig;
    }

    public void switchToSecondaryKey() {
        this.cosmosKeyCredential.key(secondaryKey);
    }
}

豆:

@Builder
@AllArgsConstructor
@Data
@Document
//@Document(collection = "testCosmos")
public class Entity {

    @Id
    @PartitionKey
    private String id;

    private String value;
}

存储库(我不想使用反应式)​​:

@Repository
    public interface SimpleRepository extends CosmosRepository<Entity, String> {
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.github.paizo</groupId>
    <artifactId>cosmos</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>cosmos</name>
    <description>Demo project for Spring Boot and Cosmos</description>

    <properties>
        <java.version>1.8</java.version>
        <azure.version>2.3.2</azure.version>
        <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-spring-boot-bom</artifactId>
                <version>${azure.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你对它为什么无法启动有什么建议吗?如果我不定义自定义 cosmos 配置类,结果不会改变

我没有使用响应式存储库,因为我想在现有的 jpa 应用程序中使用 cosmos,而 webflux 对 jpa 不满意。

4

1 回答 1

0

Azure Cosmos DB 的 Spring 数据不能用于表 API。它只能与 SQL API 一起使用。

于 2020-06-18T16:26:42.957 回答