SpringMVC 基于注解使用: 类型转换&数据格式化&数据验证-调试跟踪及自定义类型转换器

83 阅读1分钟

调试跟踪

1、Shift+Ctrl+Alt+N

查找Converter 转换类

image.png

2、Ctrl+H 后按字母排序

image.png

image.png

image.png

3、发现没有String 转换到Date 的方式 ,通过打断点

image.png

调试运行 找到

image.png

4、Shift+Ctrl+Alt+N 找到 ObjectToObjectConverter 类

通过连续打断点调试

image.png

找到类型转换(String 转换到Date)为 java.util.Date实现。

image.png


查看系统提供的所有类型转换器

通过 org/springframework/core/convert/support/GenericConversionService.java查看所有的类转换器

converters.gif

或 Evaluate Expression.. 然后 运行 converters.toString(); copy Value方法 获取所有类型转换的字符串.

ConversionService converters =
	@org.springframework.format.annotation.DateTimeFormat java.lang.Long -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@cd0ca9e,@org.springframework.format.annotation.NumberFormat java.lang.Long -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.DateTimeFormat java.time.LocalDate -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.time.LocalDate -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@2cdb57aa
	@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.time.LocalDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@1b207f7b
	@org.springframework.format.annotation.DateTimeFormat java.time.LocalTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.time.LocalTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@455d82d1
	@org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.time.OffsetDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@10efcf90
	@org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.time.OffsetTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@75e91098
	@org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.time.ZonedDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@d2f668c
	@org.springframework.format.annotation.DateTimeFormat java.util.Calendar -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@cd0ca9e
	@org.springframework.format.annotation.DateTimeFormat java.util.Date -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@cd0ca9e
	@org.springframework.format.annotation.NumberFormat java.lang.Byte -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.NumberFormat java.lang.Double -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.NumberFormat java.lang.Float -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.NumberFormat java.lang.Integer -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.NumberFormat java.lang.Short -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.NumberFormat java.math.BigDecimal -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	@org.springframework.format.annotation.NumberFormat java.math.BigInteger -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@36887606
	java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory@4421b39f
	java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@23271f66
	java.lang.Enum -> java.lang.Integer : org.springframework.core.convert.support.EnumToIntegerConverter@62a5fa84
	java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter@6e9c63c5
	java.lang.Integer -> java.lang.Enum : org.springframework.core.convert.support.IntegerToEnumConverterFactory@fc61eef
	java.lang.Long -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$LongToInstantConverter@4a94807d
	java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter@3a639f39,java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter@5e8441bd
	java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter@2301a169,java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter@1aebead6
	java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter@592a2ab2
	java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory@52705cc8
	java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@6f34b13d
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.lang.Long: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@cd0ca9e,java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Long: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.lang.String -> java.time.LocalDate: org.springframework.format.datetime.standard.TemporalAccessorParser@4b5b5be3
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.lang.String -> java.time.LocalDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@1fe2fc0e
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.lang.String -> java.time.LocalTime: org.springframework.format.datetime.standard.TemporalAccessorParser@3b6477de
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.lang.String -> java.time.OffsetDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@243c84e
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.lang.String -> java.time.OffsetTime: org.springframework.format.datetime.standard.TemporalAccessorParser@5fc0c779
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@1b355354,java.lang.String -> java.time.ZonedDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@2ac6299c
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.util.Calendar: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@cd0ca9e
	java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.util.Date: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@cd0ca9e
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Byte: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Double: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Float: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Short: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.math.BigDecimal: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> @org.springframework.format.annotation.NumberFormat java.math.BigInteger: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@79d66078
	java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter@2ccb0161
	java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter@179c11de
	java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory@3c1b932f
	java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@77e60bb6
	java.lang.String -> java.nio.charset.Charset : org.springframework.core.convert.support.StringToCharsetConverter@39cbbadd
	java.lang.String -> java.time.Duration: org.springframework.format.datetime.standard.DurationFormatter@3cc11c5b
	java.lang.String -> java.time.Instant: org.springframework.format.datetime.standard.InstantFormatter@49846078
	java.lang.String -> java.time.Month: org.springframework.format.datetime.standard.MonthFormatter@2a3dd79e
	java.lang.String -> java.time.MonthDay: org.springframework.format.datetime.standard.MonthDayFormatter@6dd1273d
	java.lang.String -> java.time.Period: org.springframework.format.datetime.standard.PeriodFormatter@572b23a4
	java.lang.String -> java.time.Year: org.springframework.format.datetime.standard.YearFormatter@463668b4
	java.lang.String -> java.time.YearMonth: org.springframework.format.datetime.standard.YearMonthFormatter@47b261eb
	java.lang.String -> java.util.Currency : org.springframework.core.convert.support.StringToCurrencyConverter@26b389f6
	java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter@5a547969
	java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter@6fc77bf4
	java.lang.String -> java.util.TimeZone : org.springframework.core.convert.support.StringToTimeZoneConverter@7b6ed491
	java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter@569cdef6
	java.nio.charset.Charset -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@4f26e65f
	java.time.Duration -> java.lang.String : org.springframework.format.datetime.standard.DurationFormatter@3cc11c5b
	java.time.Instant -> java.lang.Long : org.springframework.format.datetime.standard.DateTimeConverters$InstantToLongConverter@6324e51c
	java.time.Instant -> java.lang.String : org.springframework.format.datetime.standard.InstantFormatter@49846078
	java.time.LocalDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalDateConverter@58e33457
	java.time.LocalDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalTimeConverter@1d451e96
	java.time.Month -> java.lang.String : org.springframework.format.datetime.standard.MonthFormatter@2a3dd79e
	java.time.MonthDay -> java.lang.String : org.springframework.format.datetime.standard.MonthDayFormatter@6dd1273d
	java.time.OffsetDateTime -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToInstantConverter@40ce26d8
	java.time.OffsetDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateConverter@47355244
	java.time.OffsetDateTime -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateTimeConverter@5fd56601
	java.time.OffsetDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalTimeConverter@34d960dd
	java.time.OffsetDateTime -> java.time.ZonedDateTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToZonedDateTimeConverter@219440aa
	java.time.Period -> java.lang.String : org.springframework.format.datetime.standard.PeriodFormatter@572b23a4
	java.time.Year -> java.lang.String : org.springframework.format.datetime.standard.YearFormatter@463668b4
	java.time.YearMonth -> java.lang.String : org.springframework.format.datetime.standard.YearMonthFormatter@47b261eb
	java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter@80c689e
	java.time.ZonedDateTime -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToInstantConverter@36c051e3
	java.time.ZonedDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateConverter@a494684
	java.time.ZonedDateTime -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateTimeConverter@e5c88af
	java.time.ZonedDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalTimeConverter@366925ff
	java.time.ZonedDateTime -> java.time.OffsetDateTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToOffsetDateTimeConverter@78630a31
	java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter@12b3fa76
	java.util.Calendar -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter@2ad9c8af,java.util.Calendar -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter@4f163a75
	java.util.Calendar -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToInstantConverter@2a8dac68
	java.util.Calendar -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateConverter@67d81893
	java.util.Calendar -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateTimeConverter@19e97145
	java.util.Calendar -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalTimeConverter@14cf1700
	java.util.Calendar -> java.time.OffsetDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToOffsetDateTimeConverter@7ad3da02
	java.util.Calendar -> java.time.ZonedDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToZonedDateTimeConverter@60a15864
	java.util.Calendar -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter@3ac2102f,java.util.Calendar -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter@304ca9d3
	java.util.Currency -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@70b65a16
	java.util.Date -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter@3d11e12e,java.util.Date -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter@1b2d2be2
	java.util.Date -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter@f865655,java.util.Date -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter@7b329ecd
	java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@464b60a
	java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter@379ebaac
	java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@6419f73d
	org.springframework.core.convert.support.ArrayToArrayConverter@13fc1881
	org.springframework.core.convert.support.ArrayToCollectionConverter@63e90f20
	org.springframework.core.convert.support.ArrayToObjectConverter@20240465
	org.springframework.core.convert.support.ArrayToStringConverter@49b0706
	org.springframework.core.convert.support.ByteBufferConverter@f845f73
	org.springframework.core.convert.support.ByteBufferConverter@f845f73
	org.springframework.core.convert.support.ByteBufferConverter@f845f73
	org.springframework.core.convert.support.ByteBufferConverter@f845f73
	org.springframework.core.convert.support.CollectionToArrayConverter@8a8c888
	org.springframework.core.convert.support.CollectionToCollectionConverter@73191ffa
	org.springframework.core.convert.support.CollectionToObjectConverter@3b87a8f8
	org.springframework.core.convert.support.CollectionToStringConverter@e3de6f1
	org.springframework.core.convert.support.FallbackObjectToStringConverter@64171988
	org.springframework.core.convert.support.IdToEntityConverter@58bba691,org.springframework.core.convert.support.ObjectToObjectConverter@44a61470
	org.springframework.core.convert.support.MapToMapConverter@128e99de
	org.springframework.core.convert.support.ObjectToArrayConverter@21c634cb
	org.springframework.core.convert.support.ObjectToCollectionConverter@48b09a27
	org.springframework.core.convert.support.ObjectToOptionalConverter@37cd843c
	org.springframework.core.convert.support.ObjectToOptionalConverter@37cd843c
	org.springframework.core.convert.support.ObjectToOptionalConverter@37cd843c
	org.springframework.core.convert.support.StreamConverter@5c1185f4
	org.springframework.core.convert.support.StreamConverter@5c1185f4
	org.springframework.core.convert.support.StreamConverter@5c1185f4
	org.springframework.core.convert.support.StreamConverter@5c1185f4
	org.springframework.core.convert.support.StringToArrayConverter@600ed256
	org.springframework.core.convert.support.StringToCollectionConverter@44c42dcc

自定义类型转换器

1、写一个类 继承 Converter<String, Date> 并重写实现convert方法。

public class MyStringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        if(!StringUtils.isEmpty(source)){
            //yyyy-mm-dd  yyyy/MM/dd
           // if(Pattern.matches("/^d{4}-d{2}-d{2}$/",source))
            try {

                if(source.split("-").length ==3 || source.split("/").length==3) {
                    DateFormat df;
                    if(source.split("-").length==3){
                        df  = new SimpleDateFormat("yyyy-MM-dd");
                    }else{
                        df  = new SimpleDateFormat("yyyy/MM/dd");

                    }
                    return df.parse(source);

                }else{
                    return null;
                }
            } catch (ParseException e) {
                throw new RuntimeException(e);

            }

        }else {
           throw new RuntimeException("日期转换错误!");
        }


    }
}

2、在spring xml文件中加载响应配置

<!--配置自定义类型转换器-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"></mvc:annotation-driven>
<bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionServiceFactoryBean">
    <property name="converters" >
        <set>
            <bean class="com.kdy.com.kdy.converters.MyStringToDateConverter"></bean>
        </set>
    </property>
</bean>