简体   繁体   English

发生异常时继续处理

[英]Continue processing when an Exception occurs

I have the below code that basically reads a bunch of JSON strings, and converts them to a java object. 我有以下代码,该代码基本上读取一堆JSON字符串,并将其转换为Java对象。 My problem is if at any point, the transformation fails for any of the JSON strings, it doesn't process the others strings. 我的问题是,在任何时候,任何JSON字符串转换都失败,它不处理其他字符串。 What I need is - 我需要的是-

  1. Find the string for which the error occured. 查找发生错误的字符串。
  2. In the exception block do something to continue processing. 在异常块中,执行一些操作以继续处理。

Here is my code to convert from JSON to Java. 这是我的从JSON转换为Java的代码。

public static <T> T convertToObject(String jsonString,Class<T> classType){
    T obj = null;
    try {
        obj = objectMapper.readValue(jsonString, classType);
    } catch (Exception e) {
        throw new Exception("Unable to convert to DTO :" + e.getMessage(), e);
    } 
    return obj;
}

I think you need a custom deserializer. 我认为您需要自定义解串器。 Standard ObjectMapper will do all or nothing. 标准ObjectMapper将全部或不执行任何操作。 Read more about creating a custom deserializer for Jackson ObjectMapper here: 在这里阅读更多关于为Jackson ObjectMapper创建自定义解串器的信息:

http://www.baeldung.com/jackson-deserialization http://www.baeldung.com/jackson-deserialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM