解决json串和实体类字段不⼀致的问题
这⾥我们对json串和实体类字段不⼀致的情况进⾏⼀个测试:
⾸先,我们建⽴⼀个实体类:这⾥简单定义了name,sex,age三个属性,以及get set tostring⽅法。
public class Student {
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(Integer age) {
this.age = age;
}
fastjson忽略属性@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
⾸先⽤fastjson进⾏测试:
当字段少于实体类字段:正常通过,没有的字段会被赋予默认值
//fastjson 少字段
@Test
public void testFastjson01() {
String jsonStr = "{\"age\":18,\"name\":\"zhangsan\"}";
Student stu = JSON.parseObject(jsonStr, Student.class);
System.out.println(stu); // Student [name=zhangsan, sex=null, age=18]
}
当字段多于实体类字段:正常通过,没有的字段不会被反序列化到对象中
//fastjson 多字段
@Test
public void testFastjson02() {
String jsonStr = "{\"age\":20,\"name\":\"lisi\",\"sex\":\"男\",\"hobby\":\"basketball\"}";
Student stu = JSON.parseObject(jsonStr, Student.class);
System.out.println(stu); // Student [name=lisi, sex=男, age=20]
}
再⽤jackson进⾏测试:
当字段少于实体类字段:正常通过,没有的字段会被赋予默认值
//jackson 少字段
@Test
public void testJackson01() throws Exception{
String jsonStr = "{\"age\":18,\"name\":\"zhangsan\"}";
ObjectMapper om = new ObjectMapper();
Student stu = om.readValue(jsonStr, Student.class);
System.out.println(stu); // Student [name=zhangsan, sex=null, age=18]
}
当字段多于实体类字段:
//jackson 多字段
@Test
public void testJackson02() throws Exception{
String jsonStr = "{\"age\":20,\"name\":\"lisi\",\"sex\":\"男\",\"hobby\":\"basketball\"}";
ObjectMapper om = new ObjectMapper();
Student stu = om.readValue(jsonStr, Student.class);
System.out.println(stu);
}
此时会报错,这个错翻译过来就是,⼀个⽆法识别的field。
com.fasterxml.UnrecognizedPropertyException: Unrecognized field "hobby" (j.demo03.Student), not marked as ignorable (3 known properties: "name", "sex", "age"]) at [Source: {"age":20,"name":"lisi","sex":"男","hobby":"basketball"}; line: 1, column: 44] (through reference chain: j.demo03.Student["hobby"])
at com.fasterxml.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
at com.fasterxml.jackson.portUnknownProperty(DeserializationContext.java:817)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:958)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1324)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializ
erBase.java:1302)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:249)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3564)
at com.fasterxml.jackson.adValue(ObjectMapper.java:2580)
j.stJackson02(TestJson.java:40)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:497)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
lipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
lipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
lipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
lipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
由此可见,在默认情况下,jackson是不⽀持json串的字段多于实体类字段的,但是,jackson也提供了解决⽅案,我们在实体类上,添加注解
@JsonIgnoreProperties,这个注解有⼀个ignoreUnknown属性,默认值为false,我们可以将它改为true,这样jackson在反序列化的时候,就会忽略掉不存在的属性了。(@JsonIgnoreProperties(ignoreUnknown = true))
补充:json字段名不⼀致的问题
1.场景
项⽬开发过程中,有时候可能要对接第三⽅平台,可能就会存在字段名不⼀样的问题,⽽双⽅的字段名不⼀致,⽽代码开发已经有⼀段时间勒,如果就因为字段名不⼀样的问题,双⽅改成属性名⼀致,这样改动量太⼤,也不保险,下⾯我来接⼿⼀下,针对这种场景,如果解决。
假设:系统通过http对接,传的是json。有⼀个参数"名称"
第三⽅平台:name
开发环境:userName
直接通过实体类映射,不⽤说肯定有问题
2.解决⽅法
@JsonProperty("name")
private String userName;
在实体类的属性上增加⼀个注解就可以解决这种问题。
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。如有错误或未考虑完全的地⽅,望不吝赐教。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论