问题描述
我按照 here 中的说明使用 graalvm 构建了一个 Micronaut 应用程序。 我配置了一个自定义错误处理程序,当控制器上遇到请求正文的验证错误时,该处理程序返回一个 HttpResponse。
@Factory
public class ApiExceptionHandler {
@Produces
@Singleton
@Requires(classes = {CustomException.class,ExceptionHandler.class})
public ExceptionHandler<CustomException,HttpResponse<ApiResponse>> handleCustomException() {
return (req,ex) -> {
log.error("exception=[{}],status=[{}],message=[{}]",ex.getClass().getSimpleName(),HttpStatus.BAD_REQUEST.getCode(),ex.getMessage());
ApiResponse error = new ApiResponse.Builder(HttpStatus.BAD_REQUEST.getCode(),HttpStatus.BAD_REQUEST.getReason())
.error(new ErrorInfo(ex.getErrorCode(),ex.getErrorCode().getErrorMessage(),null))
.build();
return HttpResponse.badRequest().body(error);
};
}
}
{
"timestamp": "2021-06-28T12:54:24.292","status": 400,"description": "Bad Request","error": {
"code": "custom code","message": "Validation Error"
}
}
但是当使用 apigateway-aws-proxy
在 lambda 上测试它时,错误正文没有显示
{
"statusCode": 400,"multiValueHeaders": {
"Content-Type": [
"application/json"
]
},"body": "{}","isBase64Encoded": false
}
解决方法
在响应正文中添加 @Introspected
解决了该问题。