SpringBoot get请求发送时间参数问题(org.springframework.core.convert.ConversionFailedException: Failed to co...

今日份鸡汤:多花时间进步,跳出你厌恶的圈子,摆脱你周围厌恶的人,在你还不够强大时,你只能和你讨厌的人相处,为了配的上喜欢的东西,请拼尽全力去努力,总不能还没努力,就向生活妥协吧,半山腰太挤了,总要去山顶看看!

报错信息:
SpringBoot 发送get请求,参数中有Date类型,异常信息:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘xxxx-xx-xx’;

解决方法:
使用注解:@DateTimeFormat

    @ApiModelProperty("开始时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    @ApiModelProperty("结束时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

问题原因:
get请求将参数转化为String类型传到后台,后台的JsonFormat无法将Sting转换为Date。

总结一下:

@DateTimeFormat,spring自带的处理框架,主要用于将时间格式化 将一个字符串转成一个Date对象,一般用于前台向后台(get)0传递时间类参数 ,将传入的字符串转换为Date类型。

@JsonFormat 来源于jackson,后台向前台,将日期格式的数据格式转化为们所需要的数据。 前台向后台(post),将 Content-Type类型为application/json的字符串转换为Date类型。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容