Field error in object ‘xxx‘ on field ‘xxx‘: rejected value [xxx]

文章首发于个人博客,欢迎访问关注:https://www.lin2j.tech

GET 请求中的日期参数在使用 @JsonFormat 格式化时间后报错

@ApiModelProperty("发布时间-开始")
@JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN,timezone = Constants.GMT8)
private Date startPublishTime;
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'algoInfoQo' on field 'startPublishTime': rejected value [2021-03-29]; codes [typeMismatch.algoInfoQo.startPublishTime,typeMismatch.startPublishTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [algoInfoQo.startPublishTime,startPublishTime]; arguments []; default message [startPublishTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startPublishTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiModelProperty @com.alibaba.fastjson.annotation.JSONField java.util.Date] for value '2021-03-29'; nested exception is java.lang.IllegalArgumentException]
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:164) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]

增加 @DateTimeFormat 标注即可。

@ApiModelProperty("发布时间-开始")
@DateTimeFormat(pattern = DatePattern.NORM_DATE_PATTERN, iso = DateTimeFormat.ISO.DATE)
@JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN,timezone = Constants.GMT8)
private Date startPublishTime;
Resolved [org.springframework.web.method.annotation.ModelAttributeMethodProcessor$1: org.springframework.validation.BeanPropertyBindingResult: 4 errors<EOL>Field error in object 'order' on field 'order_id': rejected value [null]; codes [typeMismatch.order.order_id,typeMismatch.order_id,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.order_id,order_id]; arguments []; default message [order_id]]; default message [Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value [null]; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]<EOL>Field error in object 'order' on field 'phone': rejected value [null]; codes [typeMismatch.order.phone,typeMismatch.phone,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.phone,phone]; arguments []; default message [phone]]; default message [Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value [null]; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]<EOL>Field error in object 'order' on field 'buy': rejected value [null]; codes [typeMismatch.order.buy,typeMismatch.buy,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.buy,buy]; arguments []; default message [buy]]; default message [Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value [null]; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]<EOL>Field error in object 'order' on field 'total': rejected value [null]; codes [typeMismatch.order.total,typeMismatch.total,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.total,total]; arguments []; default message [total]]; default message [Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value [null]; nested excep
最新发布
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值