为什么许哟啊数据类型转换器?
ConversionService converters =
java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@1b27f3d
java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory@1d20ea2
java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@65e5d2
java.lang.Enum -> java.lang.Integer : org.springframework.core.convert.support.EnumToIntegerConverter@1ff743e
java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter@1483d7d
java.lang.Integer -> java.lang.Enum : org.springframework.core.convert.support.IntegerToEnumConverterFactory@176a4ba
java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter@bb698e
java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory@49de05
java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@631da
java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter@b0428c
java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter@6bda1
java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory@ee9b9c
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@8e88d8
java.lang.String -> java.nio.charset.Charset : org.springframework.core.convert.support.StringToCharsetConverter@1dd6f83
java.lang.String -> java.util.Currency : org.springframework.core.convert.support.StringToCurrencyConverter@1e1a986
java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter@14329a6
java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter@229207
java.lang.String -> java.util.TimeZone : org.springframework.core.convert.support.StringToTimeZoneConverter@70e46b
java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter@5a2229
java.nio.charset.Charset -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@1dbbd42
java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter@a7bb7f
java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter@b185c8
java.util.Currency -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@2b49b6
java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@15a5aa6
java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter@d516ac
java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@cb9ad
以上转换其可以完成Java大部分转换工作,但是还有部分特殊需求无法转换,比如字符串类型的转换为Date类型的。
如何实现?
一、自定义Dateconverter转换类,将其添加到Spring IoC容器中,
代码如下:
package com.jd.converter;
......
@Component//注解确保DateConverter转换类对象可可以在IoC容器中。
public class DateConverter implements Converter<String, Date> {
public Date convert(String source) {
if(source!=null&& !"".equals(source.trim())) {
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.parse(source);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
二、通过ConversionServiceFactoryBean的converters属性注册自定义的类型转换器、并将ConversionService 注册到Spring MVC 的上下文中
代码如下:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConverter"/>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>