gpt4 book ai didi

JAVA DynamoDB : not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted

转载 作者:行者123 更新时间:2023-12-01 22:17:55 27 4
gpt4 key购买 nike

我已经创建了 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;
}

这是我的 editProduct 方法:

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

}

从数据库获取产品方法:

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}

然后我收到“”message”:“内部服务器错误””,所以我检查日志文件以及我可以看到的内容:

not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException

我不知道出了什么问题。有人可以解释一下我应该做什么吗?

最佳答案

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

Item item = new Item()

使用

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

我要提到的一件事是,您可以使用 DynamoDBAutoGeneratedKey annotation 将生成的 id 的处理卸载到 DyanmoDBMapper

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

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

然后你不需要在 addProduct 方法中处理 id

编辑:您的 saveProduct 方法应该类似于

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

我怀疑您是否需要传递表名,因为这是您的 Product 对象上的注释。您不应该使用 putItem。

编辑:您的 saveProduct 方法应该采用 Product 对象,而不是 item 对象。不要使用 putItem,因为它不知道您的模型类。使用DynamoDBMapper保存功能。您不需要指定表名称,因为它是 Product 类上的注释(该表称为“产品”)。您应该使用 AmazonDynamoDB 来连接 DynamoDB。下面的方法 return 是 void,因此您需要更新 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 仅支持某些 data types .

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

编辑:您的集合 ProductKind 将需要一个类似于 this 的转换器。只需将“MyObject”更改为 ProductKinds。

关于JAVA DynamoDB : not supported; requires @DynamoDBTyped or @DynamoDBTypeConverted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47454555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com