暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringBoot控制器Controller接收日期型数据

云自由 2022-02-17
440

如果前端传到后台的是字符串,而Java类属性的Date日期类型,则会报错

Failed to convert property value of type ‘java.lang.String‘ to required type ‘java.util.Date

有多种解决方法:

一、@DateTimeFormat

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;

@Data
public class RC_jbxx {
   private Integer id;
   private String sfzhm;
   private String xm;
   private String xb;
   @DateTimeFormat(pattern = "yyyy-MM-dd")
   private Date csrq;
  }

二、初始化日期格式

 @InitBinder
   public void initBinder(WebDataBinder binder) {
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       dateFormat.setLenient(false);
       binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值,false:不能为空值
  }



文章转载自云自由,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论