Spring ConversionService adding Converters

后端 未结 9 797
别那么骄傲
别那么骄傲 2020-12-31 03:47

I searched for the below problem, but couldn\'t find an answer.

I want to use spring\'s conversion service by writing my custom converter that implements org.spri

9条回答
  •  我在风中等你
    2020-12-31 04:31

    This variant works for me. If you use java configuration, you can add your converters to existing GenericConversionService

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html

    Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.
    
        @Configuration
        class MyConfig {
    
        @Autowired
        void conversionService(GenericConversionService genericConversionService) {
            genericConversionService.addConverter(String.class, UUID.class, UUID::fromString);
            genericConversionService.addConverter(String.class, DateTime.class, DateTime::parse);
            genericConversionService.addConverter(String.class, EnumState.class, EnumState::valueOf);
        }
    }
    

提交回复
热议问题