Java DynamoDB:不支持;需要@dynamodbtyped或@dynamodbtybtypeconverted



我已经创建了crud方法,但是我有一些问题。

这是我的添加方法代码:

public Product addProduct(Product content) {
    Product item = new Product();
    item.setName(content.getName());
    item.setCalories(content.getCalories());
    item.setFat(content.getFat());
    item.setCarbo(content.getCarbo());
    item.setProtein(content.getProtein());
    item.setProductKinds(content.getProductKinds());
    item.setAuthor(content.getAuthor());
    item.setMedia(content.getMedia());
    item.setApproved(content.getApproved());

    databaseController.saveTest(item);
    logger.log("Item created");

    return item;
}

这是我的编辑方法:

public Product editProduct(Product product) {
    Product databaseProduct = databaseController.get(Product.class, product.getId());
    databaseProduct.setAllProducts(product);
    databaseController.save(databaseProduct);
    return databaseProduct;
}

在模型类中,我认为我已经做对了一切:

package pl.javamill.model.kitchen;
import com.amazonaws.services.dynamodbv2.datamodeling.*;
import pl.javamill.model.Request;
import pl.javamill.model.common.Author;
import pl.javamill.model.common.AuthorConverter;
import pl.javamill.model.common.Media;
import pl.javamill.model.common.MediaConverter;
import java.util.List;
@DynamoDBTable(tableName = "product")
public class Product extends Request {
/**
 * Id of kitchen content
 */
private String id;
/**
 * Name of product
 */
private String name;
/**
 * Calories in 100g
 */
private Integer calories;
/**
 * Fat in 100g
 */
private Double fat;
/**
 * Total carbo in 100g
 */
private Double carbo;
/**
 * Total Protein in 100g
 */
private Double protein;
/**
 * Labels of product for example gluten fee product
 */
private List<ProductKind> productKinds;
/**
 * Author of content.
 */
private Author author;
/**
 * Address of content image.
 */
private Media media;
private Boolean approved;
@DynamoDBHashKey(attributeName = "id")
@DynamoDBAutoGeneratedKey
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
@DynamoDBAttribute(attributeName = "Name")
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@DynamoDBAttribute(attributeName = "Calories")
public Integer getCalories() {
    return calories;
}
public void setCalories(Integer calories) {
    this.calories = calories;
}
@DynamoDBAttribute(attributeName = "Fat")
public Double getFat() {
    return fat;
}
public void setFat(Double fat) {
    this.fat = fat;
}
@DynamoDBAttribute(attributeName = "Carbo")
public Double getCarbo() {
    return carbo;
}
public void setCarbo(Double carbo) {
    this.carbo = carbo;
}
@DynamoDBAttribute(attributeName = "Protein")
public Double getProtein() {
    return protein;
}
public void setProtein(Double protein) {
    this.protein = protein;
}
@DynamoDBTypeConvertedEnum
@DynamoDBTypeConverted(converter = ProductKindConverter.class)
@DynamoDBAttribute(attributeName = "ProductKinds")
public List<ProductKind> getProductKinds() {
    return productKinds;
}
public void setProductKinds(List<ProductKind> productKinds) {
    this.productKinds = productKinds;
}
@DynamoDBTypeConverted(converter = AuthorConverter.class)
@DynamoDBAttribute(attributeName = "Author")
public Author getAuthor() {
    return author;
}
public void setAuthor(Author author) {
    this.author = author;
}
@DynamoDBTypeConverted(converter = MediaConverter.class)
@DynamoDBAttribute(attributeName = "Media")
public Media getMedia() {
    return media;
}
public void setMedia(Media media) {
    this.media = media;
}
@DynamoDBAttribute(attributeName = "Approved")
public Boolean getApproved() {
    return approved;
}
public void setApproved(Boolean approved) {
    this.approved = approved;
}

public void setAllProducts(Product product) {
    if (!getName().equals(product.getName())) {
        setName(product.getName());
    }
    if (!getCalories().equals(product.getCalories())) {
        setCalories(product.getCalories());
    }
    if (!getFat().equals(product.getFat())) {
        setFat(product.getFat());
    }
    if (!getCarbo().equals(product.getCarbo())) {
        setCarbo(product.getCarbo());
    }
    if (!getProtein().equals(product.getProtein())) {
        setProtein(product.getProtein());
    }
    if (!getProductKinds().equals(product.getProductKinds())) {
        setProductKinds(product.getProductKinds());
    }
    if (!getAuthor().equals(product.getAuthor())) {
        setAuthor(product.getAuthor());
    }
    if (!getMedia().equals(product.getMedia())) {
        setMedia(product.getMedia());
    }
    if (!getApproved().equals(product.getApproved())) {
        setApproved(product.getApproved());
    }
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Product product = (Product) o;
    if (name != null ? !name.equals(product.name) : product.name != null) return false;
    if (calories != null ? !calories.equals(product.calories) : product.calories != null) return false;
    if (fat != null ? !fat.equals(product.fat) : product.fat != null) return false;
    if (carbo != null ? !carbo.equals(product.carbo) : product.carbo != null) return false;
    if (protein != null ? !protein.equals(product.protein) : product.protein != null) return false;
    if (productKinds != null ? !productKinds.equals(product.productKinds) : product.productKinds != null)
        return false;
    if (author != null ? !author.equals(product.author) : product.author != null) return false;
    if (media != null ? !media.equals(product.media) : product.media != null) return false;
    return approved != null ? approved.equals(product.approved) : product.approved == null;
}
@Override
public int hashCode() {
    int result = id != null ? id.hashCode() : 0;
    result = 31 * result + (name != null ? name.hashCode() : 0);
    result = 31 * result + (calories != null ? calories.hashCode() : 0);
    result = 31 * result + (fat != null ? fat.hashCode() : 0);
    result = 31 * result + (carbo != null ? carbo.hashCode() : 0);
    result = 31 * result + (protein != null ? protein.hashCode() : 0);
    result = 31 * result + (productKinds != null ? productKinds.hashCode() : 0);
    result = 31 * result + (author != null ? author.hashCode() : 0);
    result = 31 * result + (media != null ? media.hashCode() : 0);
    result = 31 * result + (approved != null ? approved.hashCode() : 0);
    return result;
}
}

ProductKindConventor:

public class ProductKindConverter implements DynamoDBTypeConverter<String, List<ProductKind>> {
@Override
public String convert(List<ProductKind> objects) {
    //Jackson object mapper
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        String objectsString = objectMapper.writeValueAsString(objects);
        return objectsString;
    } catch (JsonProcessingException e) {
        //do something
    }
    return null;
}
@Override
public List<ProductKind> unconvert(String objectsString) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        List<ProductKind> objects = objectMapper.readValue(objectsString, new TypeReference<List<ProductKind>>(){});
        return objects;
    } catch (JsonParseException e) {
        //do something
    } catch (JsonMappingException e) {
        //do something
    } catch (IOException e) {
        //do something
    }
    return null;
}
}

在dbcontroller中保存方法:

public void saveTest(Product product){
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withRegion(regions).build();
    mapper = new DynamoDBMapper(client);
    mapper.save(product);
}

从DB方法获取产品:

public Product getTest(String id) {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withRegion(regions).build();
    mapper = new DynamoDBMapper(client);
    Product retrivedProduct = mapper.load(Product.class, id);
    return retrivedProduct;
}

单元测试通过了,一切似乎都可以,但是当我使用Postman进行测试时,我会遇到一些错误。我将产品发送为JSON,似乎是这样:

{"id":null,"name":"yoloornotyolo","calories":1000,"fat":400.0,"carbo":20.0,"protein":40.0,"productKinds":["MEAT"],"author":{"name":"Plejer Annołn","id":"testID2"},"media":{"name":"heheszki","url":"http://blabla.pl","mediaType":"IMAGE"},"approved":false}

然后我得到了"消息":"内部服务器错误",所以我检查日志文件,我可以看到的内容:

不支持;需要@dynamodbtyped或@dynamodbtypeconverted:com.amazonaws.services.dynamodbv2.datamodeling.dynamodbmappingexception

我不知道怎么了。有人可以向我解释我想做什么吗?

看到您的保存生产方法将很有用。您正在使用DynamoDBMapper注释,但看起来您正在尝试保存项目对象,而不是产品对象。而不是

Item item = new Item()

使用

Product item = new Product();
item.setName...

我要提到的一件事是,您可以使用DynamodBautogeneratedKey注释

将生成的ID处理到DyanModBmapper的处理

在您的模型类中执行此操作:

@DynamoDBHashKey(attributeName = "Id")
@DynamoDBAutoGeneratedKey
public String getId() { return id; }
public void setId(String id) { this.id = id; } 

然后您无需在addproduct方法中处理ID

编辑:您的保存生产方法应符合

的行
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
mapper.save(item)

我怀疑您需要通过表格,因为这是对您的产品对象的注释。您不应该使用putitem。

编辑:您的保存生产方法应采用产品对象,而不是项目对象。不要使用PutItem,因为这对您的模型类不了解。使用DynamoDBMapper保存功能。您不需要指定表名称,因为这是产品类上的注释(表称为"产品")。您应该使用AmazondynamodB将其接口到DynamoDB。下面的方法返回是无效的,因此您需要更新AddProduct方法。

public void saveProduct(Product item) {
logger.log("Starting to save an item");
//You need a new method to return an AmazonDynamoDB interface 
AmazonDynamoDB dynamoDB = getAmazonDynamoDB();
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
mapper.save(item)
logger.log("Item stored");
return saved;
}

编辑:您构建一个像这样的Amazondynamodb接口

   // Set to your chosen region
   AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
   .withRegion(Regions.US_WEST_2)
   .build(); 

编辑:DynamoDBMapper仅支持某些数据类型。

您那里有三种复杂的数据类型;ProductKind,作者和媒体。如果要将这些存储在数据库中,则需要为每个数据库编写一个转换器类。转换器类通常定义如何将对象转换为字符串并再次返回。

编辑:您的设置,productKind,需要一个看起来像这样的转换器。只需将" myobject"更改为ProductKinds。

对于例外情况,有一个堆栈跟踪是有用的。在中间消息中,它包含无法转换的数据类型和字段信息。

通常您不需要制作任何转换器,只需确保在所有自定义类上都有@dynamodbdocument,并且在所有使用的自定义枚举上都有@dynamodbtypeconvertedenum。

我也有这个错误,这有点误导,我们需要做的就是在引用类的类文件上使用@dynamodbdocument。

对于您的情况,只需将此注释放在productkind上,例如:

@DynamoDBDocument
class ProductKind {
    ...
}

它应该持续到DynamoDB作为产品表中的地图。

如果映射类是

,您将遇到此错误

缺少 no-arg构造函数

或具有

无效 setters/getters

不符合骆驼案例惯例

(已解决)错误:无法解开属性dynamodb

最新更新