1

I was trying to make a simple spring boot application using cosmos DB Document API (Table API is an option as well)

Following this example and using the local-emulator

Spring boot 2.3 Azure spring boot: 2.3.1

The application fail to bootstrap with:

    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]

Here a repository to reproduce the error, below the main configuration files:

@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);
    }
}

Bean:

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

    @Id
    @PartitionKey
    private String id;

    private String value;
}

Repository (I don't want to use the Reactive one):

@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>

Do you have suggestions on why it is failing to boot? The result does not change if i do not define a custom cosmos configuration class

I'm not using the reactive repository because I would like to use cosmos in an existing jpa application and webflux is not happy with jpa.

Paizo
  • 3,986
  • 30
  • 45

1 Answers1

0

Spring Data for Azure Cosmos DB CANNOT be used for Table API. It can only be used with SQL API.

RCT
  • 1,044
  • 1
  • 5
  • 12
  • Reference? I'm fine with document too, i don't see the difference in the code. Can you point me what's wrong with this code according to the documentation? – Paizo Jun 18 '20 at 18:42
  • @Paizo - this is explicitly stated right at the top of the readme, in the repo. – David Makogon Jun 18 '20 at 22:39
  • you are getting distracted because i mentioned i wanted to use the table api initially. So sorry for being misleading. As you can see from the code it uses the document api, the issue remain. – Paizo Jun 19 '20 at 08:57