When I run the following code:
public static void main(String[] args) throws JsonProcessingException {
Throwable ex = new IllegalArgumentException("something is wrong");
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writeValueAsString(ex));
}
The output to the console will show that the value in the 'cause' field in the Throwable class is null. This is because of circular reference - the class' field is initialized with the value 'this'.
I've figured that @JsonIdentityInfo is what I'm looking for, but Throwable isn't a class that I created, so I need to use a Mixin. The documentation is deprecated. After reading this discussion on GitHub, this is my latest attempt of adding a mixin:
public static void main(String[] args) throws JsonProcessingException {
Throwable ex = new IllegalArgumentException("something" +
" wrong");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(Throwable.class, MyMixn.class);
System.out.println(objectMapper.writeValueAsString(ex));
}
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator
.class, property = "cause")
public static abstract class MyMixn {
}
However, this only adds another 'cause' field to the output with the value 1 (in addition to the previous one, with the null value).
What I want is to simply have the 'cause' field output the simple class name, so I know the cause Exception.