spring.mvc.date-format和spring.jackson.date-format的区别

一.spring.mvc.date-format

之前见过这个东西,但不了解,直到这次接入支付宝..........

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors

Field error in object 'payReturn' on field 'notify_time': rejected value [2022-02-20 14:09:32]; codes [typeMismatch.payReturn.notify_time,typeMismatch.notify_time,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [payReturn.notify_time,notify_time]; arguments []; default message [notify_time]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'notify_time'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2022-02-20 14:09:32'; nested exception is java.lang.IllegalArgumentException]

Error的关键是最后这句:Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2022-02-20 14:09:32'; nested exception is java.lang.IllegalArgumentException]

PayReturn对象由支付宝异步通知填入
最终原因:This is because by default, Spring cannot convert String parameters to any date or time object.
yml配置:
1
2
3
spring:
mvc:
date-format: yyyy-MM-dd HH:mm:ss

二.spring.jackson.date-format

参见我之前写过的一篇文章

全局时间格式设置

总结:

两者的区别就是:

前者是把传入的字符串转换成特定的时间格式

后者是把时间对象以特定的格式把对应的字符串传出去