역직렬화 프로세스 중에 잠재적으로 복구 가능한 문제가 발생하면 DeserializationProblemHandler 클래스를 등록하여 호출할 수 있습니다. DeserializationProblemHandler 클래스의 handleUnknownProperty() 메서드를 구현하여 JSON을 역직렬화할 때 생성되는 오류를 처리할 수 있습니다.
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<!--?--> deserializer, Object beanOrClass, String propertyName) throws IOException
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 '" + 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 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 + "]"; } }
Property with name 'salary' doesn't exist in Class of type 'Customer' Customer [id=101, name=Ravi Chandra, address=Pune]
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)<strong> </strong>
위 내용은 Java에서 JSON을 역직렬화할 때 발생하는 오류를 처리하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!