Spring Cloud结合AWS Lambda时@ExceptionHandler失效问题求助
解决Spring Cloud AWS Lambda中自定义TestException捕获失效问题
问题根源
Spring Cloud Lambda处理API Gateway Http Api触发器时,请求流转未经过完整的Spring Web MVC Servlet栈,导致常规@ControllerAdvice/@ResponseStatus的异常处理逻辑无法直接生效。
可行解决方案
1. 适配Lambda的全局异常处理器
使用@RestControllerAdvice并实现Lambda专用的ErrorResponseExceptionHandler,确保异常响应符合API Gateway Http Api的格式:
import org.springframework.cloud.function.adapter.servlet.ErrorResponseExceptionHandler; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice public class GlobalLambdaExceptionHandler implements ErrorResponseExceptionHandler { @ExceptionHandler(TestException.class) public ResponseEntity<ErrorPayload> handleTestException(TestException ex) { ErrorPayload error = new ErrorPayload(HttpStatus.BAD_REQUEST.value(), ex.getMessage()); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .contentType(MediaType.APPLICATION_JSON) .body(error); } // 定义标准错误响应体 public static class ErrorPayload { private int statusCode; private String message; public ErrorPayload(int statusCode, String message) { this.statusCode = statusCode; this.message = message; } // Getters(序列化需要) public int getStatusCode() { return statusCode; } public String getMessage() { return message; } } }
2. 在Lambda Handler中手动捕获异常
如果全局处理仍不生效,直接在自定义Handler层捕获并构造响应:
import com.amazonaws.services.lambda.runtime.Context; import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler; import org.springframework.http.HttpStatus; public class TestLambdaHandler extends SpringBootRequestHandler<APIGatewayHttpApiV2Request, APIGatewayHttpApiV2Response> { @Override public APIGatewayHttpApiV2Response handleRequest(APIGatewayHttpApiV2Request input, Context context) { try { return super.handleRequest(input, context); } catch (TestException ex) { return APIGatewayHttpApiV2Response.builder() .statusCode(HttpStatus.BAD_REQUEST.value()) .body("{\"statusCode\":400,\"message\":\"" + ex.getMessage() + "\"}") .isBase64Encoded(false) .build(); } } }
3. 通过配置映射异常到状态码
在application.properties中添加异常与HTTP状态码的映射,同时确保TestException是运行时异常:
# 格式:spring.cloud.function.adapter.aws.error.mapping.全限定异常类名=状态码 spring.cloud.function.adapter.aws.error.mapping.com.yourpackage.TestException=400
异常类定义:
public class TestException extends RuntimeException { public TestException(String message) { super(message); } }
关键检查项
- 确保Lambda Handler继承
SpringBootRequestHandler,而非普通RequestHandler,否则Spring上下文无法加载异常处理器。 - 确认使用
APIGatewayHttpApiV2Request/APIGatewayHttpApiV2Response(对应Http Api),不要混用旧版APIGatewayProxyRequestEvent(对应REST Api)。 - 依赖检查:必须引入
spring-cloud-function-adapter-aws,若使用Web注解需引入spring-boot-starter-web并排除嵌入式Tomcat:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-aws</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
内容的提问来源于stack exchange,提问作者Thiago
相关产品推荐
相关产品推荐

