栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java字段报错:Failed to convert property value of type ‘java.lang.String‘ to required type ‘java.util.Dat

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java字段报错:Failed to convert property value of type ‘java.lang.String‘ to required type ‘java.util.Dat

遇到java接收前端日期字符串返回到后端Date字段时报错 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date
Field error in object 'user' on field 'birthday': rejected value [2019-06-19]; codes [typeMismatch.user.birthday,
typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.
support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; 
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; 
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2019-06-19'; nested exception is java.lang.IllegalArgumentException]]
解决方案一:

添加@DateTimeFormat(pattern="yyyy-MM-dd")注解

	private Integer uid;
    private String username;
    private String password;
    private String name;
    
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;
    private String sex;
解决方案二:

在controller层配置了@initBinder 自动转换日期类型的字段格式

	@InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/531826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号