创建列表的替代方法<MyObject>在 @DynamoDBTable 中不使用 dynamodbmarshalling(已弃用)

2022-01-15 00:00:00 amazon-dynamodb spring java jackson

我通过创建MyCustomMarshaller来关注此处.

MyCustomMarshaller

public class MyCustomMarshaller implements DynamoDBMarshaller<List<DemoClass>> {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final ObjectWriter writer = mapper.writer();

    @Override
    public String marshall(List<DemoClass> obj) {

        try {
            return writer.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw failure(e,
                    "Unable to marshall the instance of " + obj.getClass()
                            + "into a string");
        }
    }

    @Override
    public List<DemoClass> unmarshall(Class<List<DemoClass>> clazz, String json) {
        final CollectionType
                type =
                mapper.getTypeFactory().constructCollectionType(List.class, DemoClass.class);
        try {
            return mapper.readValue(json, type);
        } catch (Exception e) {
            throw failure(e, "Unable to unmarshall the string " + json
                    + "into " + clazz);
        }
    }
}

我的 dynamoDb 类

@DynamoDBAttribute
@DynamoDBMarshalling(marshallerClass = MyCustomMarshaller.class)
List<DemoClass> Object;

DemoClass

public class DemoClass {

    String name;

    int id;

}

所有代码都运行良好.事实是

All the codes were working great.By the thing is

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshalling 是已弃用

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshalling is deprecated

那么如何在不使用此 dynamoDBmarshalling 的情况下更改我的代码?

So how can I change my code without using this dynamoDBmarshalling?

提前致谢,
周杰伦

Thanks in Advance,
Jay

推荐答案

是的,你应该使用 DynamoDBTypeConverter

您可以从这里

为了完整起见,这里是我在链接答案中使用的示例

For completeness here is the example I used on the linked answer

// Model.java
@DynamoDBTable(tableName = "...")
public class Model {
  private String id;
  private List<MyObject> objects;

  public Model(String id, List<MyObject> objects) {
    this.id = id;
    this.objects = objects;
  }

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

  @DynamoDBTypeConverted(converter = MyObjectConverter.class)
  public List<MyObject> getObjects() { return this.objects; }
  public void setObjects(List<MyObject> objects) { this.objects = objects; }
}

-

public class MyObjectConverter implements DynamoDBTypeConverter<String, List<MyObject>> {

    @Override
    public String convert(List<Object> 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<Object> unconvert(String objectssString) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Object> objects = objectMapper.readValue(objectsString, new TypeReference<List<Object>>(){});
            return objects;
        } catch (JsonParseException e) {
            //do something
        } catch (JsonMappingException e) {
            //do something
        } catch (IOException e) {
            //do something
        }
        return null;
    }
}

相关文章