、 iが終了し、今では簡単な解決策がある場合、私は幸せになることにも表示されます(ここではそれがある:)働いて[OK]を、これは私がやった):
最初の定義:
public interface IntEnumConvertable {
public int getValue();
}
、簡単な列挙型が実装しますそれは:
public enum tester implements IntEnumConvertable{
vali(0),secondvali(1),thirdvali(5);
private final int val;
private tester(int num)
{
val = num;
}
public int getValue(){
return val;
}
}
OK]をクリックして、今、あなたは今2コンバータが必要になります、一方が他方には、より複雑である 、簡単です。単純なもの(この単純な赤ちゃんも簡単な変換を扱い、キャストが不可能なときに文字列を返します。列挙型の列挙型を列挙型にしたい場合には大丈夫です)。
public class IntegerToEnumConverterFactory implements ConverterFactory<Integer, Enum> {
@Override
public <T extends Enum> Converter<Integer, T> getConverter(Class<T> targetType) {
Class<?> enumType = targetType;
while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass();
}
if (enumType == null) {
throw new IllegalArgumentException(
"The target type " + targetType.getName() + " does not refer to an enum");
}
return new IntegerToEnum(enumType);
}
@ReadingConverter
public static class IntegerToEnum<T extends Enum> implements Converter<Integer, Enum> {
private final Class<T> enumType;
public IntegerToEnum(Class<T> enumType) {
this.enumType = enumType;
}
@Override
public Enum convert(Integer source) {
for(T t : enumType.getEnumConstants()) {
if(t instanceof IntEnumConvertable)
{
if(((IntEnumConvertable)t).getValue() == source.intValue()) {
return t;
}
}
}
return null;
}
}
}
となりましたハックの一部のために、私はmongoConverter内のコンバータの工場を登録するための任意の「programmitacly」方法を見つけるdidntのpersonnalyので、私:
public class IntegerEnumConverters {
@WritingConverter
public static class EnumToIntegerConverter implements Converter<Enum<?>, Object> {
@Override
public Object convert(Enum<?> source) {
if(source instanceof IntEnumConvertable)
{
return ((IntEnumConvertable)(source)).getValue();
}
else
{
return source.name();
}
}
}
}
、より複雑なものは、実際には、コンバータの工場ですコードを掘り出して少しキャスティングして、ここに(あなたの@Configurationクラスにこの2つの赤ちゃん関数を入れて)
@Bean
public CustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
converters.add(new IntegerEnumConverters.EnumToIntegerConverter());
// this is a dummy registration , actually it's a work-around because
// spring-mongodb doesnt has the option to reg converter factory.
// so we reg the converter that our factory uses.
converters.add(new IntegerToEnumConverterFactory.IntegerToEnum(null));
return new CustomConversions(converters);
}
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setApplicationContext(appContext);
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
mongoConverter.setCustomConversions(customConversions());
ConversionService convService = mongoConverter.getConversionService();
((GenericConversionService)convService).addConverterFactory(new IntegerToEnumConverterFactory());
mongoConverter.afterPropertiesSet();
return mongoConverter;
}
ありがとうございます。私はLowerStringToEnumConverterを実装するアイデアを使用しました – Demwis
ありがとうございました!それは非常に春のデータは簡単な方法を提供していないことが有線です – ruX
これは非常に有用だった。 –