spring-batch-rest icon indicating copy to clipboard operation
spring-batch-rest copied to clipboard

Date job parameters provided as string

Open goatfryed opened this issue 5 years ago • 1 comments

We'v a job that requires a creationDate as date parameter.

While JobParamUtil checks for date parameters, the issue seems to be that the properties map is never filled with date properties as the deserializer treats them as string by default.

Did I miss any way to force a date or is this feature currently not supported?

goatfryed avatar Jul 15 '20 14:07 goatfryed

Hi,

as you correctly stated, Date parameters which are specified when starting a job are currently provided as String parameters to the job since they are conveyed via the Map<String, Object> field in JobConfig. We rely on Jackson's default JSON deserialization, which is to convert ISO-formatted dates to Strings if the target type is an Object.

Without changing spring-batch-rest, here are two way how you could resolve this:

  • Change your jobs to accept both Date and String parameters. If a String parameter is supplied, you convert it to a Date.
  • Configure a custom Jackson deserializer in Spring Boot which converts an Object to a Date if it is of type String and its format is a valid date, see https://stackoverflow.com/questions/18313323/how-do-i-call-the-default-deserializer-from-a-custom-deserializer-in-jackson (to call the default Object deserializer) and combine this with an appropriate String to Date converter, see https://www.baeldung.com/java-string-to-date

Please let me know whether this is viable for you and whether you maybe have already found another workaround.

Thanks

chrisgleissner avatar Jul 31 '20 08:07 chrisgleissner