Java Jackson反序列化AWS Lambda嵌套异常JSON对象的方法
cause Field Deserialization for Lambda Exceptions with Jackson Great question! Handling the recursive nested cause structure from AWS Lambda's error JSON response with Jackson is straightforward once you set up your ExceptionRep POJO correctly. Here's how to do it step by step:
1. Define the ExceptionRep POJO with Recursive cause Field
First, make sure your POJO mirrors the Lambda error JSON structure, including a self-referential cause field to handle nested exceptions. Jackson natively supports recursive type deserialization, so no special tricks are needed here—just match the field names properly:
import java.util.List; public class ExceptionRep { private String errorMessage; private String errorType; private List<String> stackTrace; private ExceptionRep cause; // Recursive reference for nested causes // Required: Jackson needs a no-arg constructor for deserialization public ExceptionRep() {} // Getters and Setters (or use Lombok to auto-generate these) public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public String getErrorType() { return errorType; } public void setErrorType(String errorType) { this.errorType = errorType; } public List<String> getStackTrace() { return stackTrace; } public void setStackTrace(List<String> stackTrace) { this.stackTrace = stackTrace; } public ExceptionRep getCause() { return cause; } public void setCause(ExceptionRep cause) { this.cause = cause; } }
If you use Lombok to reduce boilerplate, you can simplify this with @Data and @NoArgsConstructor (to ensure the required no-arg constructor exists):
import lombok.Data; import lombok.NoArgsConstructor; import java.util.List; @Data @NoArgsConstructor public class ExceptionRep { private String errorMessage; private String errorType; private List<String> stackTrace; private ExceptionRep cause; }
2. Deserialize the Lambda Error JSON with Jackson
Once your POJO is set up, use Jackson's ObjectMapper to deserialize the JSON response directly. Jackson will automatically handle any depth of nested cause fields—whether there's no cause, one level, or multiple layers:
import com.fasterxml.jackson.databind.ObjectMapper; public class LambdaErrorDeserializer { public static void main(String[] args) throws Exception { // Sample Lambda error JSON with nested cause String lambdaErrorJson = """ { "errorMessage": "Top-level error message", "errorType": "com.example.MyException", "stackTrace": ["com.example.MyClass.myMethod(MyClass.java:123)"], "cause": { "errorMessage": "Root cause message", "errorType": "java.sql.SQLException", "stackTrace": ["java.sql.DriverManager.getConnection(DriverManager.java:404)"], "cause": null } } """; ObjectMapper objectMapper = new ObjectMapper(); ExceptionRep exceptionRep = objectMapper.readValue(lambdaErrorJson, ExceptionRep.class); // Access nested causes ExceptionRep rootCause = exceptionRep.getCause(); if (rootCause != null) { System.out.println("Root cause message: " + rootCause.getErrorMessage()); } } }
Key Notes
- Field Name Matching: Ensure your POJO field names exactly match the keys in Lambda's error JSON (
errorMessage,errorType,stackTrace,cause). If you need to use different field names, annotate them with@JsonProperty("json-key-name"). - Null Handling: Jackson will automatically set
causetonullwhen the JSON doesn't include a cause or the cause is explicitlynull. - Stack Trace Type: Lambda returns
stackTraceas an array of strings, so usingList<String>(orString[]) in your POJO works perfectly with Jackson's default deserialization.
内容的提问来源于stack exchange,提问作者kibowki

