• objectMaaper 反序列化json字段多于或少于实体处理


    两种方式

    1 忽略json上未知的字段

      设置实体类注解

    @JsonIgnoreProperties(ignoreUnknown = true)
    
    public class Foo {
        ...
    }

    2 配置objectMapper

    public static <T> T JsonNode2Entity(JsonNode node,Class t ,boolean throwErr) throws RuntimeException {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);  //忽略大小写
            objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);      //json 为SNAKE_CASE  实体为lowerCase Camel
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);  //对未知的属性不处理
            try {
                return (T) objectMapper.readValue(node.toString(), t);
    
            } catch (java.lang.Exception e) {
                e.printStackTrace();
                if(throwErr){
                    //ThrowWarnning(ErrorCode.Err_ParseParam);
                    throw new RuntimeException("参数解析失败");
                }
                return null;
            }
        }
    例:
    snake_case json

    {
        "context": {
            "year": "2019"
        },
        "data": {
            "GDXMZD_CZZD_GDCZLB_TITL": "项目明细",
            "GDXMZD_SFWY": "0",
            "GDXMZD_KFXG": "0",
            "GDXMZD_LBID": "%",
            "GDXMZD_XMXZ": "0",
            "GDXMZD_XMMC": "新增的项目",
            "GDXMZD_KFBG": "0",
            "GDXMZD_GSSM": null,
            "GDXMZD_QJY": "",
            "CREATOR": "admin",
            "LASTMODIFIER": "admin",
            "GDXMZD_CZZD_GDCZLB_CZZD": "vo",
            "ID": "40282e816d15be06016d15c259650000",
            "GDXMZD_XMLX": "C",
            "GDXMZD_KPMX": "1",
            "GDXMZD_JSXS": "0",
            "GDXMZD_JSGS": null,
            "GDXMZD_SFBT": "0",
            "GDXMZD_XSSX": 98,
            "GDXMZD_CZZD": "xmmx",
            "GDXMZD_DECN": 0,
            "GDXMZD_XMBH": "GDZCZY_XM01",
            "GDXMZD_IFENUM": "0",
            "GDXMZD_LENG": "888",
            "GDXMZD_IFCZ": "1",
            "GDXMZD_IFPUB": "0"
        }
    }

    转换的实体

    @Data
    @Entity
    @DynamicInsert
    @DynamicUpdate
    
    @Table(name = "figdxmzd2019")
    public class GDXMZD {
        @Id
        @GenericGenerator(name = "uuid",strategy = "uuid")
        @GeneratedValue(generator = "uuid")
        @Column(name = "id", nullable = false, length = 36)
        public String id;
        @Column(name = "gdxmzd_lbid", nullable = false, length = 36)
        public String gdxmzdLbid;
        @Column(name = "gdxmzd_xmbh", nullable = false, length = 60)
        public String gdxmzdXmbh;
        @Column(name = "gdxmzd_xmmc", nullable = false, length = 60)
        public String gdxmzdXmmc;
        @Column(name = "gdxmzd_xmlx", nullable = false, length = -1)
        public String gdxmzdXmlx;
        @Column(name = "gdxmzd_xmxz", nullable = false, length = -1)
        public String gdxmzdXmxz;
        @Column(name = "gdxmzd_jsgs", length = 1000)
        public String gdxmzdJsgs;
        @Column(name = "gdxmzd_gssm", length = 1000)
        public String gdxmzdGssm;
        @Column(name = "gdxmzd_xssx", length = 3)
        public int gdxmzdXssx;
        @Column(name = "gdxmzd_leng", nullable = false)
        public int gdxmzdLeng;
        @Column(name = "gdxmzd_decn", nullable = false)
        public int gdxmzdDecn;
        @Column(name = "gdxmzd_ifpub", nullable = false, length = -1)
        public String gdxmzdIfpub;
        @Column(name = "gdxmzd_kpmx", nullable = false, length = -1)
        public String gdxmzdKpmx;
        @Column(name = "gdxmzd_kfbg", nullable = false, length = -1)
        public String gdxmzdKfbg;
        @Column(name = "gdxmzd_sfbt", nullable = false, length = -1)
        public String gdxmzdSfbt;
        @Column(name = "gdxmzd_kfxg", nullable = false, length = -1)
        public String gdxmzdKfxg;
        @Column(name = "gdxmzd_ifcz", nullable = false, length = -1)
        public String gdxmzdIfcz;
        @Column(name = "gdxmzd_czzd", nullable = false, length = 20)
        public String gdxmzdCzzd;
        @Column(name = "gdxmzd_ifenum", nullable = false, length = -1)
        public String gdxmzdIfenum;
        @Column(name = "gdxmzd_jsxs", nullable = false, length = -1)
        public String gdxmzdJsxs;
        @Column(name = "gdxmzd_sfwy", nullable = false, length = -1)
        public String gdxmzdSfwy;
        @Column(name = "gdxmzd_qjy", nullable = false, length = -1)
        public String gdxmzdQjy;
        @Column(name = "createdtime")
        public LocalDateTime createdtime;
        @Column(name = "lastmodifiedtime")
        public LocalDateTime lastmodifiedtime;
        @Column(name = "lastmodifier", length = 128)
        public String lastmodifier;
        @Column(name = "creator", length = 128)
        public String creator;
    
        public  GDXMZD(){}
    
        public GDXMZD(String xmbh){
            this.gdxmzdXmbh=xmbh;
        }
    
    
    
        @PrePersist
        protected void onCreate() {
            createdtime = CAFContext.current.getCurrentDateTime();
            creator = CAFContext.current.getUserId();
        }
    
        @PreUpdate
        protected void onUpdate() {
            lastmodifiedtime = CAFContext.current.getCurrentDateTime();
            lastmodifier = CAFContext.current.getUserId();
        }
    
    }

    反向转换(序列化)

     ObjectMapper m = new ObjectMapper();
            m.setPropertyNamingStrategy(new UpperSnakCasePropertyNamingStrategy());
            m.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            try {
                m.writeValue(System.out, gdxmzdRepository.findAll());
            } catch (IOException e) {
                e.printStackTrace();
            }

    参考:

    https://stackoverflow.com/questions/5455014/ignoring-new-fields-on-json-objects-using-jackson

  • 相关阅读:
    spring boot +mybatis 操作sqlite数据库
    katalon studio教程之通过录制/回放创建测试用例
    #katalon studio# 安装和设置(Installation and Setup)
    NET Core 基于Aspect Injector 实现面向AOP编程
    NET Core 3.1使用AutoMapper实现对象映射
    给NET core 智能感知提示安装中文汉化包
    代码注释规范
    软件升级版本号迭代规范-Semantic Versioning
    使用阿里云的Helm私有仓库
    Helm操作指南
  • 原文地址:https://www.cnblogs.com/wolbo/p/11496576.html
Copyright © 2020-2023  润新知