首页 > 解决方案 > SpringBoot - Java AWS SDK 2 DynamoDB 增强客户端和开发工具问题

问题描述

我正在使用 Spring Boot 2.17 和 java sdk 以及 dynamodb-enhanced '2.13.8'。

我正在与增强型客户端调用这样的项目:

public Product readProductById(String id) {
    Key key = Key.builder()
            .partitionValue(id)
            .build();
    Product product =  productTable.getItem(key);
    return product;
}

调用此错误时会导致:

class de.xxx.productsapi.db.Product cannot be cast to class de.xxx.productsapi.db.Product (de.xxx.productsapi.db.Product is in unnamed module of loader 'app'; de.xxx.productsapi.db.Product is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @19e19c7e)
java.lang.ClassCastException: class de.xxx.productsapi.db.Product cannot be cast to class de.xxx.productsapi.db.Product (de.xxx.productsapi.db.Product is in unnamed module of loader 'app'; de.xxx.productsapi.db.Product is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @19e19c7e)

切换livereload to enabled: false并没有帮助,但完全删除了 devtools 工作。但这是一个不令人满意的解决方案,因为我想使用 devtools。

感谢帮助

标签: javaamazon-web-servicesspring-bootamazon-dynamodb

解决方案


Spring Boot DevTools 重启功能是使用两个类加载器实现的。项目由重启类加载器加载,库由基类加载器加载。

使用该文件META-INF/spring-devtools.properties将 DynamoDB jar 移动到RestartClassloader

restart.include.dynamodb=/dynamodb-[\\w\\d-\.]+\.jar

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload


推荐阅读