Spring参数解析错误

在Spring应用中,出现了一个BeanPropertyBindingResult的2个错误,涉及到userParam对象的endTime和startTime字段。错误指出无法将字符串'2021-03-30T16:00:00.000Z'和'2021-03-29T16:00:00.000Z'转换为Date类型,导致ConversionFailedException。问题源于Swagger请求参数格式与Controller中参数类型不匹配。

报错信息

2021-03-31 09:54:22.383 [http-nio-8103-exec-11] WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object ‘userParam’ on field ‘endTime’: rejected value [2021-03-30T16:00:00.000Z]; codes [typeMismatch.userParam.endTime,typeMismatch.endTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userParam.endTime,endTime]; arguments []; default message [endTime]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘endTime’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiModelProperty java.util.Date] for value ‘2021-03-30T16:00:00.000Z’; nested exception is java.lang.IllegalArgumentException]
Field error in object ‘userParam’ on field ‘startTime’: rejected value [2021-03-29T16:00:00.000Z]; codes [typeMismatch.userParam.startTime,typeMismatch.startTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userParam.startTime,startTime]; arguments []; default message [startTime]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘startTime’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiModelProperty java.util.Date] for value ‘2021-03-29T16:00:00.000Z’; nested exception is java.lang.IllegalArgumentException]]

接收参数的DTO

@Data
public class UserParam {
    @ApiModelProperty("手机号")
	private String mobile;
    @ApiModelProperty("开始时间")
	private Date startTime;
	@ApiModelProperty("结束时间")
	private Date endTime;
}

Controller


```java

```java

```java

```java
@RestController
@RequestMapping("/v1/client")
@Api(value = "C端用户管理", tags = "C端用户管理相关接口")
public class ClientController {
    @Autowired
	IClientService iClientService;
	@GetMapping("/clientQueryList")
	public PageResult clientQueryList(UserParam userParam,
									@ApiParam @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
									@ApiParam  @RequestParam(value = "pageNum",required = false,defaultValue = "10") Long pageNum) {

		return iClientService.queryClientList(userParam, pageIndex, pageNum);



	}
}

Swagger 请求参数格式

在这里插入图片描述

报错信息: 无法将对应的字符串解析成日期类型
解决方法: 参见链接

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值