Ich versuche, Jackson zu erhalten, um eine JSON-codierte benutzerdefinierte Ausnahme deserialize, und es schlägt fehl, wenn die Ausnahme den Stacktrace deaktiviert.jackson: deserialize eine benutzerdefinierte Ausnahme
Arbeitsbeispiel:
public static class CustomException extends Exception {
public CustomException(String msg) {
super(msg);
}
}
@Test
public void testSerializeAndDeserializeCustomException() throws Exception {
log.info("Test: testSerializeAndDeserializeCustomException");
CustomException ex1 = new CustomException("boom");
ObjectMapper om = new ObjectMapper();
String json = om.writerFor(CustomException.class).writeValueAsString(ex1);
assertNotNull(json);
log.info("JSON: {}", json);
CustomException ex2 = om.readerFor(CustomException.class).readValue(json);
assertNotNull(ex2);
assertEquals(ex2.getMessage(), ex1.getMessage());
}
Problem Beispiel:
public static class CustomNoStackException extends Exception {
public CustomNoStackException(String msg) {
super(msg, null, true, false);
}
}
@Test
public void testSerializeAndDeserializeCustomNoStackException() throws Exception {
log.info("Test: testSerializeAndDeserializeCustomNoStackException");
CustomNoStackException ex1 = new CustomNoStackException("boom");
ObjectMapper om = new ObjectMapper();
String json = om.writerFor(CustomNoStackException.class).writeValueAsString(ex1);
assertNotNull(json);
log.info("JSON: {}", json);
CustomNoStackException ex2 = om.readerFor(CustomNoStackException.class).readValue(json);
assertNotNull(ex2);
assertEquals(ex2.getMessage(), ex1.getMessage());
}
Im zweiten Fall wirft readValue(json)
tatsächlich die verpackte CustomNoStackException
in einem IOException
.
Was mache ich falsch?
Vielen Dank. Hättest du eine Idee, wie du es reparieren kannst? Vielleicht überschreiben 'getCause()' und kommentieren es '@ JsonIgnore' - scheint zu funktionieren, aber ist es eine gute Idee? – Hank