您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在Java中处理反序列化JSON时生成的错误的方法是什么?

2025/10/7 1:47:20发布24次查看
当在反序列化过程中遇到可能可恢复的问题时,可以注册deserializationproblemhandler 类来进行调用。我们可以通过实现deserializationproblemhandler 类的handleunknownproperty()方法来处理在反序列化json时生成的错误。
语法public boolean handleunknownproperty(deserializationcontext ctxt, jsonparser p, jsondeserializer deserializer, object beanorclass, string propertyname) throws ioexception
exampleimport java.io.*;import com.fasterxml.jackson.core.*;import com.fasterxml.jackson.databind.*;import com.fasterxml.jackson.databind.deser.*;public class deserializationerrortest { public static void main(string[] args) throws jsonmappingexception, jsongenerationexception, ioexception { string jsonstring = "{\"id\":\"101\", \"name\":\"ravi chandra\", \"address\":\"pune\", \"salary\":\"40000\" }"; objectmapper objectmapper = new objectmapper(); deserializationproblemhandler deserializationproblemhandler = new unmarshallingerrorhandler(); objectmapper.addhandler(deserializationproblemhandler); customer customer = objectmapper.readvalue(jsonstring, customer.class); system.out.println(customer); }}// unmarshallingerrorhandler classclass unmarshallingerrorhandler extends deserializationproblemhandler { @override public boolean handleunknownproperty(deserializationcontext ctxt, jsonparser jp, jsondeserializer deserializer, object beanorclass, string propertyname) throws ioexception, jsonprocessingexception { boolean result = false; super.handleunknownproperty(ctxt, jp, deserializer, beanorclass, propertyname); system.out.println("property with name '" + propertyname + "' doesn't exist in class of type '" + beanorclass.getclass().getname() + "'"); return true; // returns true to inform the deserialization process that we can handle the error and it can continue deserializing and returns false, if we want to stop the deserialization immediately. }}// customer classclass customer { private int id; private string name; private string address; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } @override public string tostring() { return "customer [id=" + id + ", name=" + name + ", address=" + address + "]"; }}
当返回true时的输出property with name 'salary' doesn't exist in class of type 'customer'customer [id=101, name=ravi chandra, address=pune]
当返回false时的输出property with name 'salary' doesn't exist in class of type 'customer'exception in thread "main" com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field "salary" (class customer), not marked as ignorable (3 known properties: "id", "address", "name"])at [source: (string)"{"id":"101", "name":"ravi chandra", "address":"pune", "salary":"40000" }"; line: 1, column: 65] (through reference chain: customer["salary"]) at com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception.from(unrecognizedpropertyexception.java:61)at com.fasterxml.jackson.databind.deserializationcontext.handleunknownproperty(deserializationcontext.java:840)at com.fasterxml.jackson.databind.deser.std.stddeserializer.handleunknownproperty(stddeserializer.java:1179)at com.fasterxml.jackson.databind.deser.beandeserializerbase.handleunknownproperty(beandeserializerbase.java:1592) at com.fasterxml.jackson.databind.deser.beandeserializerbase.handleunknownvanilla(beandeserializerbase.java:1570) at com.fasterxml.jackson.databind.deser.beandeserializer.vanilladeserialize(beandeserializer.java:294)at com.fasterxml.jackson.databind.deser.beandeserializer.deserialize(beandeserializer.java:151)at com.fasterxml.jackson.databind.objectmapper._readmapandclose(objectmapper.java:4202)at com.fasterxml.jackson.databind.objectmapper.readvalue(objectmapper.java:3205)at com.fasterxml.jackson.databind.objectmapper.readvalue(objectmapper.java:3173)at deserializationerrortest.main(deserializationerrortest.java:12)
以上就是在java中处理反序列化json时生成的错误的方法是什么?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product