两种方式
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(); }
参考:
1 https://stackoverflow.com/questions/5455014/ignoring-new-fields-on-json-objects-using-jackson