首页 > Java > java教程 > 正文

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

WBOY
发布: 2023-09-01 12:13:06
转载
706 人浏览过

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

当在反序列化过程中遇到可能可恢复的问题时,可以注册DeserializationProblemHandler 类来进行调用。我们可以通过实现DeserializationProblemHandler 类的handleUnknownProperty()方法来处理在反序列化JSON时生成的错误。

语法

public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<!--?--> deserializer, Object beanOrClass, String propertyName) throws IOException
登录后复制

Example

import 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\" }";
      <strong>ObjectMapper </strong>objectMapper = new ObjectMapper();
      DeserializationProblemHandler deserializationProblemHandler = new UnMarshallingErrorHandler();
      objectMapper.addHandler(deserializationProblemHandler);
      Customer customer = objectMapper.readValue(jsonString, Customer.class);
      System.out.println(customer);
   }
}
// UnMarshallingErrorHandler class<strong>
</strong>class 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 &#39;" + propertyName + "&#39; doesn&#39;t exist in Class of type &#39;" + beanOrClass.getClass().getName() + "&#39;");
      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 class
class 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 &#39;salary&#39; doesn&#39;t exist in Class of type &#39;Customer&#39;
Customer [id=101, name=Ravi Chandra, address=Pune]
登录后复制

当返回false时的输出

Property with name &#39;salary&#39; doesn&#39;t exist in Class of type &#39;Customer&#39;
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)<strong>
</strong>
登录后复制

以上是在Java中处理反序列化JSON时生成的错误的方法是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板