Issues while deserializing exception/throwable using Jackson in Java

前端 未结 6 1799
無奈伤痛
無奈伤痛 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:17

    Try using polymorphism so that jackson deserializer knows what kind of Throwable to create:

    /**
     * Jackson module to serialize / deserialize Throwable
     */
    public class ThrowableModule extends SimpleModule {
      public ThrowableModule() {
        super("Throwable", new Version(1, 0, 0, null, null, null));
      }
    
      @Override
      public void setupModule(SetupContext context) {
        context.setMixInAnnotations(Throwable.class, ThrowableAnnotations.class);
      }
    
      /**
       * Add annotation to Throwable so that the class name is serialized with the instance data.
       */
      @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
      static abstract class ThrowableAnnotations {
      }
    }
    

提交回复
热议问题