Ok, now you will now need 2 converters , one is simple ,the other is more complex. the simple one (this simple baby is also handling the simple convert and returns a string when cast is not possible, that is great if you want to have enum stored as strings and for enum that are numbers to be stored as integers) :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(); } } } }更复杂的一个实际上是转换器工厂:the more complex one , is actually a converter factory :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; } }}现在对于hack部分,我个人没有找到任何编程方式"在mongoConverter中注册转换器工厂,因此我在代码中进行了一点点铸造,在这里(将这2个婴儿函数放在您的@Configuration类)and now for the hack part , i personnaly didnt find any "programmitacly" way to register a converter factory within a mongoConverter , so i digged in the code and with a little casting , here it is (put this 2 babies functions in your @Configuration class) @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; } 这篇关于Spring -Mongodb将枚举存储/检索为int而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 15:02