Issues while deserializing exception/throwable using Jackson in Java

前端 未结 6 1775
無奈伤痛
無奈伤痛 2021-02-19 13:42

I am facing issues while deserializing Exception and Throwable instances using Jackson (version 2.2.1). Consider the following snippet:



        
6条回答
  •  广开言路
    2021-02-19 14:16

    Add this:

    objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    

    And make out of the deserialized exception the same way, as for the first time:

    System.out.println( objectMapper.writeValueAsString( throwable ) );
    

    I used the following code:

    public static void main( String[] args ) throws IOException
    {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure( SerializationFeature.INDENT_OUTPUT, true );
        objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setVisibility( PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY );
        objectMapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY );
    
        try
        {
            Integer.parseInt( "String" );
        }
        catch( NumberFormatException e )
        {
            Throwable throwable = objectMapper.readValue( objectMapper.writeValueAsString( e ), Throwable.class );
            System.out.println( objectMapper.writeValueAsString( throwable ) );
        }
    }
    

    Added this jars: jackson-annotations-2.2.0.jar, jackson-core-2.2.0.jar and jackson-databind-2.2.0.jar.

    After execution, the following is printed:

    {
    "@class" : "java.lang.NumberFormatException",
    "detailMessage" : "For input string: \"String\"",
    "cause" : null,
    "stackTrace" : [ {
        "declaringClass" : "java.lang.NumberFormatException",
        "methodName" : "forInputString",
        "fileName" : "NumberFormatException.java",
        "lineNumber" : 48,
        "className" : "java.lang.NumberFormatException",
        "nativeMethod" : false
    }, {
        "declaringClass" : "java.lang.Integer",
        "methodName" : "parseInt",
        "fileName" : "Integer.java",
        "lineNumber" : 449,
        "className" : "java.lang.Integer",
        "nativeMethod" : false
    }, {
        "declaringClass" : "java.lang.Integer",
        "methodName" : "parseInt",
        "fileName" : "Integer.java",
        "lineNumber" : 499,
        "className" : "java.lang.Integer",
        "nativeMethod" : false
    }, {
        "declaringClass" : "com.sample.bla.Main",
        "methodName" : "main",
        "fileName" : "Main.java",
        "lineNumber" : 24,
        "className" : "com.sample.bla.Main",
        "nativeMethod" : false
    }, {
        "declaringClass" : "sun.reflect.NativeMethodAccessorImpl",
        "methodName" : "invoke0",
        "fileName" : "NativeMethodAccessorImpl.java",
        "lineNumber" : -2,
        "className" : "sun.reflect.NativeMethodAccessorImpl",
        "nativeMethod" : true
    }, {
        "declaringClass" : "sun.reflect.NativeMethodAccessorImpl",
        "methodName" : "invoke",
        "fileName" : "NativeMethodAccessorImpl.java",
        "lineNumber" : 39,
        "className" : "sun.reflect.NativeMethodAccessorImpl",
        "nativeMethod" : false
    }, {
        "declaringClass" : "sun.reflect.DelegatingMethodAccessorImpl",
        "methodName" : "invoke",
        "fileName" : "DelegatingMethodAccessorImpl.java",
        "lineNumber" : 25,
        "className" : "sun.reflect.DelegatingMethodAccessorImpl",
        "nativeMethod" : false
    }, {
        "declaringClass" : "java.lang.reflect.Method",
        "methodName" : "invoke",
        "fileName" : "Method.java",
        "lineNumber" : 597,
        "className" : "java.lang.reflect.Method",
        "nativeMethod" : false
    }, {
        "declaringClass" : "com.intellij.rt.execution.application.AppMain",
        "methodName" : "main",
        "fileName" : "AppMain.java",
        "lineNumber" : 120,
        "className" : "com.intellij.rt.execution.application.AppMain",
        "nativeMethod" : false
        } ],
        "message" : "For input string: \"String\"",
        "localizedMessage" : "For input string: \"String\""
    }
    

提交回复
热议问题