spring-batch-rest
spring-batch-rest copied to clipboard
Date job parameters provided as string
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?
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
DateandStringparameters. If aStringparameter is supplied, you convert it to aDate. - Configure a custom Jackson deserializer in Spring Boot which converts an
Objectto aDateif it is of typeStringand 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 defaultObjectdeserializer) and combine this with an appropriateStringtoDateconverter, 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