• SpringMVC 集成 jackson,日志格式报错:org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value


    org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '2012-12-12 12:01:01': not a valid representation (error: Can not parse date "2012-12- - 故宫博物院 - 博客园
    https://www.cnblogs.com/suizhikuo/p/8393781.html

    关于jackson中时间字符串的转换 - masquelo的专栏 - CSDN博客
    https://blog.csdn.net/masquejava/article/details/10556281

    org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from Str - kero921的博客 - CSDN博客
    https://blog.csdn.net/kero921/article/details/79835627

    package org.jeecgframework.core.common.controller;
    
    import java.io.IOException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.codehaus.jackson.JsonParser;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.map.DeserializationContext;
    import org.codehaus.jackson.map.JsonDeserializer;
    import org.springframework.util.StringUtils;
    /**
     * 解决@RequestBody接收json数据,Jackson 反序列化Date格式
     * @author scott
     *
     */
    public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
        private SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
        @Override
        public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            String text = jp.getText();
    
            if (StringUtils.hasText(text)) {
                try {
                    if (text.indexOf(":") == -1 && text.length() == 10) {
                        return this.dateFormat.parse(text);
                    } else if (text.indexOf(":") > 0 && text.length() == 19) {
                        return this.datetimeFormat.parse(text);
                    } else {
                        throw new IllegalArgumentException("Could not parse date, date format is error ");
                    }
                } catch (ParseException ex) {
                    IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
                    iae.initCause(ex);
                    throw iae;
                }
            } else {
                return null;
            }
        }
    
    }
        //商品有效期限
        @JSONField(format="yyyy-MM-dd HH:mm:ss")
        private java.util.Date goodsdates;
    
        @Column(name ="goodsdates",nullable=true,length=20)
        @JsonDeserialize(using = CustomJsonDateDeserializer.class)
        public java.util.Date getGoodsdates() {
            return goodsdates;
        }
    
        public void setGoodsdates(java.util.Date goodsdates) {
            this.goodsdates = goodsdates;
        }
  • 相关阅读:
    用Navicat运行一个比较大的.sql文件时报错:[Err] 2006
    访问laravel的api接口返回200和html代码,没有返回打印的一些数据
    英文字母和中文汉字在不同字符集编码下的字节数
    PHP curl详解
    php判断图片是否损坏
    png转为jpg
    win10快速解决警告:libpng warning: iCCP: known incorrect sRGB profile
    PHP:cURL error 60: SSL certificate unable to get local issuer certificate
    windows mysql服务出错
    go 切片对数组的修改,切片的扩容
  • 原文地址:https://www.cnblogs.com/rgqancy/p/11004667.html
Copyright © 2020-2023  润新知