spring,get请求中带date日期格式参数,后台无法转换的问题

时间:2021-03-19 10:38:02

今天遇到个很奇怪的问题。前端 的查询条件中带有日期范围日期的格式 是 yyyy-MM-dd HH:mm 结果后台报错

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'taskModel' on field 'createTimeEnd': rejected value [08-12-2019 00:00]; codes [typeMismatch.taskModel.createTimeEnd,typeMismatch.createTimeEnd,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taskModel.createTimeEnd,createTimeEnd]; arguments []; default message [createTimeEnd]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTimeEnd'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value '08-12-2019 00:00'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'taskModel' on field 'createTimeStart': rejected value [07-16-2019 00:00]; codes [typeMismatch.taskModel.createTimeStart,typeMismatch.createTimeStart,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taskModel.createTimeStart,createTimeStart]; arguments []; default message [createTimeStart]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTimeStart'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value '07-16-2019 00:00'; nested exception is java.lang.IllegalArgumentException]

但是其他的请求里面也是这个格式,后台在实体类上面加注解

@JsonFormat(pattern = "yyyy-MM-dd HH:mm")

都是可以接收到并且解析的。

然后我发现,其他的请求是post 类型的,然后 这个是get 请求。

网上查了下发现,要使用  MM/dd/yyyy HH:mm 这种斜杠的格式才行

<el-date-picker
v-model="listQuery.taskDate" value-format="MM/dd/yyyy HH:mm" type="daterange" range-separator="-"
start-placeholder="开始同步时间" end-placeholder="结束同步时间"
class="component-inner-date">
</el-date-picker>