了解自定义处理程序和 SpringBootApiGatewayRequestHandler 之间的区别

Understanding difference between Custom Handler and SpringBootApiGatewayRequestHandler

我是 Spring Cloud Function 的新手,发现它是开发基于 FaaS 的解决方案的最佳解决方案之一。我专门为 API 网关后端的 AWS Lambda 服务编写应用程序。我 运行 在我的测试应用程序中遇到了一个非常有趣的问题,它与处理程序有关。我的测试应用程序与编写为 -

的自定义处理程序配合得很好
public class UserProfileHandler extends SpringBootRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
}

在 AWS Lambda 中配置为处理程序时效果很好。然后我遇到了 org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler,它在 Spring Cloud Function 依赖项中可用,所以我想摆脱 UserProfileHandler 因此我将 AWS Lambda 中的处理程序配置更改为 org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler 而不是...UserProfileHandler 现在 lambda 失败并显示以下错误消息。有人 运行 遇到过这个问题吗?

{
  "errorMessage": "java.util.Optional cannot be cast to com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent",
  "errorType": "java.lang.ClassCastException",
  "stackTrace": [
    "com.transformco.hs.css.userprofile.function.UserProfileFunction.apply(UserProfileFunction.java:16)",
    "org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry$FunctionInvocationWrapper.invokeFunction(BeanFactoryAwareFunctionRegistry.java:499)",
    "org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry$FunctionInvocationWrapper.lambda$doApply(BeanFactoryAwareFunctionRegistry.java:543)",
    "reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107)",
    "reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:121)",
    "reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99)",
    "reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162)",
    "reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162)",
    "reactor.core.publisher.BlockingIterable$SubscriberIterator.onSubscribe(BlockingIterable.java:218)",
    "reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)",
    "reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)",
    "reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70)",
    "reactor.core.publisher.InternalFluxOperator.subscribe(InternalFluxOperator.java:53)",
    "reactor.core.publisher.BlockingIterable.iterator(BlockingIterable.java:80)",
    "org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.result(SpringBootRequestHandler.java:59)",
    "org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:52)",
    "org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler.handleRequest(SpringBootApiGatewayRequestHandler.java:140)",
    "org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler.handleRequest(SpringBootApiGatewayRequestHandler.java:43)"
  ]
}

Ganesh,我相信你已经在SCF的Github中提出了这个问题。因此,正如我在那里所说的那样,我们最近做了一些改进,通过添加 Getting Started guide.

完善了示例和修改的文档

也就是说,有了新的通用请求处理程序,您不再需要提供 AWS 请求处理程序的实现,包括 SpringBootApiGatewayRequestHandler

只需编写包含功能 bean 的引导应用程序即可

@SpringBootApplication
public class FunctionConfiguration {

    public static void main(String[] args) {
        SpringApplication.run(FunctionConfiguration.class, args);
    }

    @Bean
    public Function<String, String> uppercase() {
        return value -> value.toUpperCase();
    }
}

。 . .并将 org.springframework.cloud.function.adapter.aws.FunctionInvoker 指定为 AWS 仪表板中的处理程序。剩下的交给我们。