Jim*_*mes 8 java amazon-dynamodb swagger-codegen
我有一个正在扩展的类型HashMap<String, String>。根据此处的文档,可以为该类型添加自定义转换器。但它似乎不起作用。的内容hashMap不会被转换,输出如下所示;
"summary": {
"en": null
},
Run Code Online (Sandbox Code Playgroud)
知道如何转换Label及其字段及其哈希图的内容吗?
家长
@DynamoDbBean(converterProviders = {
CustomAttributeConverterProvider.class,
DefaultAttributeConverterProvider.class})
public class Summary extends BaseEntry {
private @Valid Label summary = null;
}
Run Code Online (Sandbox Code Playgroud)
孩子
@DynamoDbBean(converterProviders = {
CustomAttributeConverterProvider.class,
DefaultAttributeConverterProvider.class})
public class Label extends HashMap<String, String> {
private @Valid String en = null;
}
Run Code Online (Sandbox Code Playgroud)
HashMap属性转换器
public class HashMapAttributeConverter implements AttributeConverter<Map<String, String>> {
private static AttributeConverter<Map<String, String>> mapConverter;
/** Default constructor. */
public HashMapAttributeConverter() {
mapConverter =
MapAttributeConverter.builder(EnhancedType.mapOf(String.class, String.class))
.mapConstructor(HashMap::new)
.keyConverter(StringStringConverter.create())
.valueConverter(StringAttributeConverter.create())
.build();
}
@Override
public AttributeValue transformFrom(Map<String, String> input) {
return mapConverter.transformFrom(input);
}
@Override
public Map<String, String> transformTo(AttributeValue input) {
return mapConverter.transformTo(input);
}
@Override
public EnhancedType<Map<String, String>> type() {
return mapConverter.type();
}
@Override
public AttributeValueType attributeValueType() {
return mapConverter.attributeValueType();
}
}
Run Code Online (Sandbox Code Playgroud)
自定义属性转换器提供者
public class CustomAttributeConverterProvider implements AttributeConverterProvider {
private final List<AttributeConverter<?>> customConverters =
Arrays.asList(new HashMapAttributeConverter());
private final Map<EnhancedType<?>, AttributeConverter<?>> customConvertersMap;
private final AttributeConverterProvider defaultProvider =
DefaultAttributeConverterProvider.create();
public CustomAttributeConverterProvider() {
customConvertersMap =
customConverters.stream().collect(Collectors.toMap(AttributeConverter::type, c -> c));
}
@Override
public <T> AttributeConverter<T> converterFor(EnhancedType<T> enhancedType) {
return (AttributeConverter<T>)
customConvertersMap.computeIfAbsent(enhancedType, defaultProvider::converterFor);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 1
@Override
public <T> AttributeConverter<T> converterFor(EnhancedType<T> type) {
// in this method you have to return only your converter based on type
// otherwise null should be returned. It will fix your issue.
}
Run Code Online (Sandbox Code Playgroud)