我正在尝试实现自定义类型转换器,但由于上面的错误消息而未能实现。 使用-已弃用的编组器可以按预期工作
我想知道是否可以使用@DynamoDBTypeConverted来实现转换,或者是否被@DynamoDBMarshalling困扰。
package com.acme;
import com.amazonaws.services.dynamodbv2.datamodeling.*;
import com.amazonaws.services.dynamodbv2.datamodeling.marshallers.ObjectToMapMarshaller;
import com.amazonaws.services.dynamodbv2.datamodeling.unmarshallers.ObjectUnmarshaller;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import java.util.Objects;
@DynamoDBDocument
public class MyDynamoDBEntity {
@DynamoDBTypeConverted(converter = Id.Converter.class)
//@DynamoDBMarshalling(marshallerClass = Id.Marshaller.class)
protected Id id;
public Id getId() {
return id;
}
public void setId(final Id id) {
this.id = id;
}
public static class Converter implements DynamoDBTypeConverter<AttributeValue, MyDynamoDBEntity> {
private ItemConverter itemConverter = ConversionSchemas.V2.getConverter(new ConversionSchema.Dependencies());
@Override
public AttributeValue convert(final MyDynamoDBEntity object) {
if (object == null) {
return null;
}
return new ObjectToMapMarshaller(itemConverter).marshall(object);
}
@Override
public MyDynamoDBEntity unconvert(AttributeValue value) {
try {
// non empty object
if (value.getM() != null && value.getM().size() > 0) {
final ObjectUnmarshaller unmarshaller = new ObjectUnmarshaller(itemConverter, MyDynamoDBEntity.class);
return (MyDynamoDBEntity) unmarshaller.unmarshall(value);
} else {
return null;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static class Id {
private String value;
public Id(final String value) {
this.value = value;
}
public String toValue() {
return value;
}
public static Id of(String s) {
return new Id(s);
}
public static class Converter implements DynamoDBTypeConverter<String, Id> {
@Override
public String convert(final Id value) {
return value.toValue();
}
@Override
public Id unconvert(final String value) {
return Id.of(value);
}
}
public static class Marshaller implements DynamoDBMarshaller<Id> {
@Override
public String marshall(final Id value) {
return value.toValue();
}
@Override
public Id unmarshall(final Class<Id> clazz, final String value) {
return Id.of(value);
}
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Id id = (Id) o;
return Objects.equals(value, id.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
}
感谢您指出正确的方向。