Spring comes with predefined converters.
The built-in converters are not public, so we cannot find them in API docs. To know what types can be converted by default please use your IDE to view hierarchy of Converter interface.
Following code list all converters defined in Spring core:
package com.logicbig.example;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import java.util.Set;
public class SpringBuiltInConvertersFinder {
public static void main(String[] args) {
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AssignableTypeFilter(Converter.class));
Set<BeanDefinition> components = provider.findCandidateComponents("org/springframework");
components.stream()
.map(BeanDefinition::getBeanClassName)
.forEach(System.out::println);
}
}
Outputorg.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToCalendarConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToDateConverter org.springframework.format.datetime.joda.JodaTimeConverters$LongToReadableInstantConverter org.springframework.format.datetime.joda.JodaTimeConverters$CalendarToReadableInstantConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToMutableDateTimeConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalDateTimeConverter org.springframework.format.datetime.joda.JodaTimeConverters$LocalDateTimeToLocalTimeConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateToReadableInstantConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToInstantConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToDateMidnightConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalDateConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLongConverter org.springframework.format.datetime.joda.JodaTimeConverters$DateTimeToLocalTimeConverter org.springframework.format.datetime.joda.JodaTimeConverters$LocalDateTimeToLocalDateConverter org.springframework.format.datetime.standard.DateTimeConverters$InstantToLongConverter org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToOffsetDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$CalendarToOffsetDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$CalendarToZonedDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToZonedDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateConverter org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToInstantConverter org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateConverter org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalDateConverter org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToInstantConverter org.springframework.format.datetime.standard.DateTimeConverters$CalendarToInstantConverter org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalTimeConverter org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateConverter org.springframework.format.datetime.standard.DateTimeConverters$LongToInstantConverter org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter org.springframework.core.convert.support.PropertiesToStringConverter org.springframework.core.convert.support.StringToUUIDConverter org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber org.springframework.core.convert.support.ZoneIdToTimeZoneConverter org.springframework.core.convert.support.NumberToNumberConverterFactory$NumberToNumber org.springframework.core.convert.support.StringToBooleanConverter org.springframework.core.convert.support.StringToCharacterConverter org.springframework.core.convert.support.ObjectToStringConverter org.springframework.core.convert.support.EnumToStringConverter org.springframework.core.convert.support.StringToCharsetConverter org.springframework.core.convert.support.EnumToIntegerConverter org.springframework.core.convert.support.StringToCurrencyConverter org.springframework.core.convert.support.NumberToCharacterConverter org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter org.springframework.core.convert.support.StringToLocaleConverter org.springframework.core.convert.support.CharacterToNumberFactory$CharacterToNumber org.springframework.core.convert.support.StringToPropertiesConverter org.springframework.core.convert.support.StringToTimeZoneConverter org.springframework.core.convert.converter.ConvertingComparator$ConversionServiceConverter org.springframework.core.serializer.support.SerializingConverter org.springframework.core.serializer.support.DeserializingConverter
Converters registered with DefaultConversionService
DefaultConversionService doesn't provide some public methods to see what Converters are registered by default, but we can use reflection to find that out:
package com.logicbig.example;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import java.lang.reflect.Field;
public class ConvertersRegisteredFinder {
public static void main(String[] args) throws Exception {
DefaultConversionService service = new DefaultConversionService();
Field converters = GenericConversionService.class.getDeclaredField("converters");
converters.setAccessible(true);
Object o = converters.get(service);
System.out.println(o.toString().replaceAll("@.+\n", "\n"));
}
}
OutputConversionService converters = java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter java.lang.Enum -> java.lang.Integer : org.springframework.core.convert.support.EnumToIntegerConverter java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter java.lang.Integer -> java.lang.Enum : org.springframework.core.convert.support.IntegerToEnumConverterFactory java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory java.lang.String -> java.nio.charset.Charset : org.springframework.core.convert.support.StringToCharsetConverter java.lang.String -> java.util.Currency : org.springframework.core.convert.support.StringToCurrencyConverter java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter java.lang.String -> java.util.TimeZone : org.springframework.core.convert.support.StringToTimeZoneConverter java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter java.nio.charset.Charset -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter java.util.Currency -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter org.springframework.core.convert.support.ArrayToArrayConverter org.springframework.core.convert.support.ArrayToCollectionConverter org.springframework.core.convert.support.ArrayToObjectConverter org.springframework.core.convert.support.ArrayToStringConverter org.springframework.core.convert.support.ByteBufferConverter org.springframework.core.convert.support.ByteBufferConverter org.springframework.core.convert.support.ByteBufferConverter org.springframework.core.convert.support.ByteBufferConverter org.springframework.core.convert.support.CollectionToArrayConverter org.springframework.core.convert.support.CollectionToCollectionConverter org.springframework.core.convert.support.CollectionToObjectConverter org.springframework.core.convert.support.CollectionToStringConverter org.springframework.core.convert.support.FallbackObjectToStringConverter org.springframework.core.convert.support.IdToEntityConverter org.springframework.core.convert.support.MapToMapConverter org.springframework.core.convert.support.ObjectToArrayConverter org.springframework.core.convert.support.ObjectToCollectionConverter org.springframework.core.convert.support.ObjectToOptionalConverter org.springframework.core.convert.support.StreamConverter org.springframework.core.convert.support.StreamConverter org.springframework.core.convert.support.StreamConverter org.springframework.core.convert.support.StreamConverter org.springframework.core.convert.support.StringToArrayConverter org.springframework.core.convert.support.StringToCollectionConverter
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 4.0.7.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|