controler类pojo类型入参日期类型处理
- 方法一:
pojo类属性添加
//备用yyyy-MM-dd HH:mm:ss
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
核心原理(关键调用信息)
org.springframework.web.method.support.InvocableHandlerMethod#getMethodArgumentValues
org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor#bindRequestParameters
org.springframework.validation.DataBinder#applyPropertyValues
org.springframework.beans.TypeConverterDelegate#convertIfNecessary(java.lang.String, java.lang.Object, java.lang.Object, java.lang.Class<T>, org.springframework.core.convert.TypeDescriptor)
//将原始值转换为目标值;TypeDescriptor很重要,如果在pojo的属性上加了注解,typeDescriptor=@org.springframework.format.annotation.DateTimeFormat java.util.Date
return (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor);
–默认的转换器
org.springframework.format.support.DefaultFormattingConversionService
**–spring中,pojo的属性注解获取地方:**org.springframework.core.convert.Property#resolveAnnotations
- 方法二:
controler类注册属性编辑
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CustomDateEditor editor = new CustomDateEditor(df, true);//true表示允许为空,false反之
binder.registerCustomEditor(Date.class, editor);
}
原理:WebDataBinderFactory
org.springframework.web.method.annotation.InitBinderDataBinderFactory#initBinder
- 方法三:土
@RequestMapping(value = "test")
public String test(ModelMap model, String date) throws ParseException {
Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
// ...接下来怎么做咯
return "test";
}
- 类型转换器
ConversionService converters =
@org.springframework.format.annotation.DateTimeFormat java.lang.Long -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@2ce1ae8c,@org.springframework.format.annotation.NumberFormat java.lang.Long -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.DateTimeFormat java.time.LocalDate -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.time.LocalDate -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@69192cae
@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.time.LocalDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@575ec654
@org.springframework.format.annotation.DateTimeFormat java.time.LocalTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.time.LocalTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@6d05e280
@org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.time.OffsetDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@4ed28a2b
@org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.time.OffsetTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@130a08b0
@org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime -> java.lang.String: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.time.ZonedDateTime -> java.lang.String : org.springframework.format.datetime.standard.TemporalAccessorPrinter@f3f1403
@org.springframework.format.annotation.DateTimeFormat java.util.Calendar -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@2ce1ae8c
@org.springframework.format.annotation.DateTimeFormat java.util.Date -> java.lang.String: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@2ce1ae8c
@org.springframework.format.annotation.NumberFormat java.lang.Byte -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.NumberFormat java.lang.Double -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.NumberFormat java.lang.Float -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.NumberFormat java.lang.Integer -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.NumberFormat java.lang.Short -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.NumberFormat java.math.BigDecimal -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
@org.springframework.format.annotation.NumberFormat java.math.BigInteger -> java.lang.String: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@9a9f128
java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory@a648196
java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@4f563651
java.lang.Enum -> java.lang.Integer : org.springframework.core.convert.support.EnumToIntegerConverter@4a42c8b0
java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter@66d4f37b
java.lang.Integer -> java.lang.Enum : org.springframework.core.convert.support.IntegerToEnumConverterFactory@ca448bd
java.lang.Long -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$LongToInstantConverter@786ea8ac
java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter@6b2e2f3d,java.lang.Long -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$LongToCalendarConverter@3fc05a2
java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter@39a08e6f,java.lang.Long -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$LongToDateConverter@15659ca0
java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter@4eb1e96a
java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory@1ec43f90
java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@7d9720da
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.lang.Long: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@2ce1ae8c,java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Long: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.lang.String -> java.time.LocalDate: org.springframework.format.datetime.standard.TemporalAccessorParser@64bfc2e9
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.lang.String -> java.time.LocalDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@4decfedc
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.LocalTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.lang.String -> java.time.LocalTime: org.springframework.format.datetime.standard.TemporalAccessorParser@7782e856
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.OffsetDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.lang.String -> java.time.OffsetDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@64628733
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.OffsetTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.lang.String -> java.time.OffsetTime: org.springframework.format.datetime.standard.TemporalAccessorParser@6927e6
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.time.ZonedDateTime: org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory@6d4eea7c,java.lang.String -> java.time.ZonedDateTime: org.springframework.format.datetime.standard.TemporalAccessorParser@6f9f8dea
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.util.Calendar: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@2ce1ae8c
java.lang.String -> @org.springframework.format.annotation.DateTimeFormat java.util.Date: org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory@2ce1ae8c
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Byte: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Double: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Float: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Short: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.math.BigDecimal: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> @org.springframework.format.annotation.NumberFormat java.math.BigInteger: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@2b1ccd4
java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter@77b3a531
java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter@70d1af64
java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory@4892a3ce
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@82138e1
java.lang.String -> java.nio.charset.Charset : org.springframework.core.convert.support.StringToCharsetConverter@78d2a399
java.lang.String -> java.time.Duration: org.springframework.format.datetime.standard.DurationFormatter@6997fb13
java.lang.String -> java.time.Instant: org.springframework.format.datetime.standard.InstantFormatter@5a306612
java.lang.String -> java.time.MonthDay: org.springframework.format.datetime.standard.MonthDayFormatter@79d15535
java.lang.String -> java.time.Period: org.springframework.format.datetime.standard.PeriodFormatter@2a9b2978
java.lang.String -> java.time.YearMonth: org.springframework.format.datetime.standard.YearMonthFormatter@2008a7d0
java.lang.String -> java.util.Currency : org.springframework.core.convert.support.StringToCurrencyConverter@2f66b5b9
java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter@17139ee5
java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter@7ba38860
java.lang.String -> java.util.TimeZone : org.springframework.core.convert.support.StringToTimeZoneConverter@53556cf1
java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter@a0b1a8b
java.nio.charset.Charset -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@2489f37b
java.time.Duration -> java.lang.String : org.springframework.format.datetime.standard.DurationFormatter@6997fb13
java.time.Instant -> java.lang.Long : org.springframework.format.datetime.standard.DateTimeConverters$InstantToLongConverter@49880771
java.time.Instant -> java.lang.String : org.springframework.format.datetime.standard.InstantFormatter@5a306612
java.time.LocalDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalDateConverter@3e922044
java.time.LocalDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$LocalDateTimeToLocalTimeConverter@7ab2baa
java.time.MonthDay -> java.lang.String : org.springframework.format.datetime.standard.MonthDayFormatter@79d15535
java.time.OffsetDateTime -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToInstantConverter@5cab4e8c
java.time.OffsetDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateConverter@7abcf45a
java.time.OffsetDateTime -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalDateTimeConverter@60b1851e
java.time.OffsetDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToLocalTimeConverter@74c06a4e
java.time.OffsetDateTime -> java.time.ZonedDateTime : org.springframework.format.datetime.standard.DateTimeConverters$OffsetDateTimeToZonedDateTimeConverter@41a86cc7
java.time.Period -> java.lang.String : org.springframework.format.datetime.standard.PeriodFormatter@2a9b2978
java.time.YearMonth -> java.lang.String : org.springframework.format.datetime.standard.YearMonthFormatter@2008a7d0
java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter@7312cb7c
java.time.ZonedDateTime -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToInstantConverter@27d0dd02
java.time.ZonedDateTime -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateConverter@1de6bba5
java.time.ZonedDateTime -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalDateTimeConverter@516bf330
java.time.ZonedDateTime -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToLocalTimeConverter@7ed47cae
java.time.ZonedDateTime -> java.time.OffsetDateTime : org.springframework.format.datetime.standard.DateTimeConverters$ZonedDateTimeToOffsetDateTimeConverter@fc364cc
java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter@7f6c4e0b
java.util.Calendar -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter@5f543c25,java.util.Calendar -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToLongConverter@6591a567
java.util.Calendar -> java.time.Instant : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToInstantConverter@509073
java.util.Calendar -> java.time.LocalDate : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateConverter@45f7a62b
java.util.Calendar -> java.time.LocalDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalDateTimeConverter@1ed756ba
java.util.Calendar -> java.time.LocalTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToLocalTimeConverter@43ae5f37
java.util.Calendar -> java.time.OffsetDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToOffsetDateTimeConverter@47bb244f
java.util.Calendar -> java.time.ZonedDateTime : org.springframework.format.datetime.standard.DateTimeConverters$CalendarToZonedDateTimeConverter@2b4edd4d
java.util.Calendar -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter@eba5ae0,java.util.Calendar -> java.util.Date : org.springframework.format.datetime.DateFormatterRegistrar$CalendarToDateConverter@66495417
java.util.Currency -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@16002f52
java.util.Date -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter@4f3d0c7,java.util.Date -> java.lang.Long : org.springframework.format.datetime.DateFormatterRegistrar$DateToLongConverter@393a2424
java.util.Date -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter@35e198bb,java.util.Date -> java.util.Calendar : org.springframework.format.datetime.DateFormatterRegistrar$DateToCalendarConverter@1455c545
java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@569de857
java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter@105abe44
java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@1d66de27
org.springframework.core.convert.support.ArrayToArrayConverter@6d8116cd
org.springframework.core.convert.support.ArrayToCollectionConverter@7ae1b563
org.springframework.core.convert.support.ArrayToObjectConverter@22a7ebfb
org.springframework.core.convert.support.ArrayToStringConverter@6a304cd4
org.springframework.core.convert.support.ByteBufferConverter@6bd2d271
org.springframework.core.convert.support.ByteBufferConverter@6bd2d271
org.springframework.core.convert.support.ByteBufferConverter@6bd2d271
org.springframework.core.convert.support.ByteBufferConverter@6bd2d271
org.springframework.core.convert.support.CollectionToArrayConverter@3f0c8ce5
org.springframework.core.convert.support.CollectionToCollectionConverter@463d6abc
org.springframework.core.convert.support.CollectionToObjectConverter@7d58bcf6
org.springframework.core.convert.support.CollectionToStringConverter@7f0157f8
org.springframework.core.convert.support.FallbackObjectToStringConverter@1fb6f196
org.springframework.core.convert.support.IdToEntityConverter@2d05acd,org.springframework.core.convert.support.ObjectToObjectConverter@7a1a9c82
org.springframework.core.convert.support.MapToMapConverter@310085b5
org.springframework.core.convert.support.ObjectToArrayConverter@17bfff37
org.springframework.core.convert.support.ObjectToCollectionConverter@1576a22d
org.springframework.core.convert.support.ObjectToOptionalConverter@2b7e87d
org.springframework.core.convert.support.ObjectToOptionalConverter@2b7e87d
org.springframework.core.convert.support.ObjectToOptionalConverter@2b7e87d
org.springframework.core.convert.support.StreamConverter@60bd99ec
org.springframework.core.convert.support.StreamConverter@60bd99ec
org.springframework.core.convert.support.StreamConverter@60bd99ec
org.springframework.core.convert.support.StreamConverter@60bd99ec
org.springframework.core.convert.support.StringToArrayConverter@f620821
org.springframework.core.convert.support.StringToCollectionConverter@603d7b39