我的枚举在mongodb中存储为int(来自C#app)。现在在Java中,当我尝试检索它们时,它会抛出异常(似乎枚举只能从字符串值转换)。我有什么方法可以做到吗?
当我将一些集合保存到mongodb(来自Java)时,它会将枚举值转换为字符串(而不是它们的值/基数)。有没有可用的覆盖?
这可以通过在类级别编写mongodb-converter来实现,但我不想为每个类编写mondodb-converter,因为这些枚举在许多不同的类中。
那么我们在战场上有什么东西吗?
答案 0 :(得分:9)
经过长期挖掘spring-mongodb转换器代码后, 好的,我完成了,现在它正在工作:)这里是(如果有更简单的解决方案,我会很高兴看到,这就是我做的):
首先定义:
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;
}
}
好的,现在你需要2个转换器,一个很简单, 另一个更复杂。简单的一个(这个简单的宝贝也处理简单的转换并在无法进行强制转换时返回一个字符串,如果你想将枚举存储为字符串,并且枚举是将数字存储为整数,那就太棒了):
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();
}
}
}
}
更复杂的一个,实际上是转换器工厂:
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;
}
}
}
现在对于黑客部分,我个人没有找到任何“programmitacly”方式在mongoConverter中注册一个转换器工厂,所以我挖掘代码并用一点点铸造,这里是(把这2个婴儿的功能放入你的@Configuration类)
@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;
}
答案 1 :(得分:2)