What property type converters are built into Java – Spring?
What is the supported value for the targetType
parameter in Spring Framework methods PropertyResolver.getProperty(String key, Class targetType) ? I’m looking for a list of types supported by default.
The following types are obviously valid:
- The locale class
- Comprehensive class
- File class
- URI.class
Example:
// works
Locale myLocale = propertyResolver.getProperty("my.locale", Locale.class);
I found this list:
http://www.logicbig.com/how-to/spring-framework/spring-converters-list/
But it doesn’t list File
or URI
, so it doesn’t look complete. Also, I prefer the official documentation.
I > reference in Nothing found either.
Solution
I tried the following to figure it out:
Define a bean in my context.xml
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
Then I do:
public static void main(String[] args) {
try (ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml")) {
ConversionService service = context.getBean(ConversionService.class);
System.out.println(service);
}
}
I see
ConversionService converters =
java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@520a3426
java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory@6b09bb57
java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@5f9d02cb
java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter@3e9b1010
java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter@63753b6d
java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory@527740a2
java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@3108bc
java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter@6536e911
java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter@370736d9
java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory@18eed359
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@13a5fe33
java.lang.String -> java.nio.charset.Charset : org.springframework.core.convert.support.StringToCharsetConverter@185d8b6
java.lang.String -> java.util.Currency : org.springframework.core.convert.support.StringToCurrencyConverter@335eadca
java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter@6c3708b3
java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter@eec5a4a
java.lang.String -> java.util.TimeZone : org.springframework.core.convert.support.StringToTimeZoneConverter@61f8bee4
java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter@6ddf90b0
java.nio.charset.Charset -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@67784306
java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter@7b49cea0
java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter@887af79
java.util.Currency -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@210366b4
java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@6f1fba17
java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter@2b2948e2
java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@57536d79
org.springframework.core.convert.support.ArrayToArrayConverter@4b4523f8
org.springframework.core.convert.support.ArrayToCollectionConverter@3b0143d3
org.springframework.core.convert.support.ArrayToObjectConverter@7791a895
org.springframework.core.convert.support.ArrayToStringConverter@1f28c152
org.springframework.core.convert.support.ByteBufferConverter@4f970963
org.springframework.core.convert.support.ByteBufferConverter@4f970963
org.springframework.core.convert.support.ByteBufferConverter@4f970963
org.springframework.core.convert.support.ByteBufferConverter@4f970963
org.springframework.core.convert.support.CollectionToArrayConverter@5a8e6209
org.springframework.core.convert.support.CollectionToCollectionConverter@731a74c
org.springframework.core.convert.support.CollectionToObjectConverter@67b92f0a
org.springframework.core.convert.support.CollectionToStringConverter@6325a3ee
org.springframework.core.convert.support.FallbackObjectToStringConverter@6e0e048a
org.springframework.core.convert.support.IdToEntityConverter@7fac631b,org.springframework.core.convert.support.ObjectToObjectConverter@5b87ed94
org.springframework.core.convert.support.MapToMapConverter@369f73a2
org.springframework.core.convert.support.ObjectToArrayConverter@3a5ed7a6
org.springframework.core.convert.support.ObjectToCollectionConverter@2b9627bc
org.springframework.core.convert.support.ObjectToOptionalConverter@5bc79255
org.springframework.core.convert.support.StreamConverter@65e2dbf3
org.springframework.core.convert.support.StreamConverter@65e2dbf3
org.springframework.core.convert.support.StreamConverter@65e2dbf3
org.springframework.core.convert.support.StreamConverter@65e2dbf3
org.springframework.core.convert.support.StringToArrayConverter@7d907bac
org.springframework.core.convert.support.StringToCollectionConverter@1d16f93d
These are the default converter available in DefaultConversionService.
I noticed that there is a FallbackObjectToStringConverter converts almost anything to a string. Therefore, URIs
and File
to String
will always be valid. The opposite seems to be done by handled by ObjectToObjectConverter, which is smart enough to get an object from a string representation using a constructor.
I guess depending on the libraries and Spring dependencies you’re using, they might register more converters here.