微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Micronaut 错误正文未显示在 AWS Lambda 上

如何解决Micronaut 错误正文未显示在 AWS Lambda 上

我按照 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
}

如何在 aws lambda 函数显示错误正文?

解决方法

在响应正文中添加 @Introspected 解决了该问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。