AWS Lambda反序列化器/LinkedHashmap无法强制转换 [英] AWS Lambda de-serializer / LinkedHashmap cannot cast

查看:76
本文介绍了AWS Lambda反序列化器/LinkedHashmap无法强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java/AWS等的新手,AWS lambda并不会反序列化我认为是简单的示例.

I'm new to Java/AWS etc., and AWS lambda we not de-serialize what I think is a trivial example.

API网关日志,该日志将JSON发送到我的lambda:

The API Gateway log, which is sending JSON to my lambda:

请求测试请求的执行日志 Mon Sep 11 18:04:06 UTC 2017:开始执行请求:test-invoke-request Mon Sep 11 18:04:06 UTC 2017:HTTP方法:POST,资源路径:/ Mon Sep 11 18:04:06 UTC 2017:方法请求路径:{} Mon Sep 11 18:04:06 UTC 2017:方法请求查询字符串:{} Mon Sep 11 18:04:06 UTC 2017:方法请求标头:{Content-Type = application/json} Mon Sep 11 18:04:06 UTC 2017:转换前的方法请求正文:{"input":"richard在这里"} Mon Sep 11 18:04:06 UTC 2017:端点请求URI:

Execution log for request test-request Mon Sep 11 18:04:06 UTC 2017 : Starting execution for request: test-invoke-request Mon Sep 11 18:04:06 UTC 2017 : HTTP Method: POST, Resource Path: / Mon Sep 11 18:04:06 UTC 2017 : Method request path: {} Mon Sep 11 18:04:06 UTC 2017 : Method request query string: {} Mon Sep 11 18:04:06 UTC 2017 : Method request headers: {Content-Type=application/json} Mon Sep 11 18:04:06 UTC 2017 : Method request body before transformations: {"input":"richard was here"} Mon Sep 11 18:04:06 UTC 2017 : Endpoint request URI: https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-2:003374398906:function:uppertolowercase/invocations Mon Sep 11 18:04:06 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=****************************************************************************************************************************************************************************************************************************************************************************************************************************************8acc24, X-Amz-Date=20170911T180406Z, x-amzn-apigateway-api-id=8fjgcr51va, X-Amz-Source-Arn=arn:aws:execute-api:us-west-2:003374398906:8fjgcr51va/null/POST/, Accept=application/json, User-Agent=AmazonAPIGateway_8fjgcr51va, X-Amz-Security-Token=xxxxxxx//////////wEaDM3BrDjbuGSwMFT5lCK3AyBVM7duW9tzEe/bcWqWiYNIxkpMjTdDZaW3U32asJ4qvntUsRpjfSIhLWAds74XhzHbI7GzXMuV3zQYCIiYRX0ZL1s524J7mETGFN4OredlGln7CCEs3WR417UaPB4XLh6E0v71Srpg4a1kG7KB6426gN9CvXR0mmYbwpl9qtp9bovLgIoVnjYbx5j7qd7Fa3U4wQOqcHonlKtN/uDXMFW+vfHOJyu3gQUyvoVpMuXrQSF/gptEXG+l0v4+v1exq67sp8G5d8h1kAQTNQep8Q19kyOi9hWbNDyU7FzXWvfSRX7f9n6NGIuZ6LYIF3g2kPFY [TRUNCATED] Mon Sep 11 18:04:06 UTC 2017 : Endpoint request body after transformations: {"input":"richard was here"} Mon Sep 11 18:04:06 UTC 2017 : Sending request to https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-2:003374398906:function:uppertolowercase/invocations Mon Sep 11 18:04:06 UTC 2017 : Received response. Integration latency: 73 ms Mon Sep 11 18:04:06 UTC 2017 : Endpoint response body before transformations: {"errorMessage":"java.util.LinkedHashMap cannot be cast to com.morethanheroic.uppercase.domain.UppercaseRequest","errorType":"java.lang.ClassCastException","stackTrace":["com.morethanheroic.uppercase.UppercaseFunction.apply(UppercaseFunction.java:13)","org.springframework.cloud.function.support.FluxFunction.lambda$apply$0(FluxFunction.java:42)","reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107)","reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:90)","reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:159)","reactor.core.publisher.BlockingIterable$SubscriberIterator.onSubscribe(BlockingIterable.java:214)","reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)","reactor.core.publisher.FluxJust.subscribe(FluxJust.java:67)","reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63)","reactor.core.pu [TRUNCATED] Mon Sep 11 18:04:06 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=9a0ca35b-971b-11e7-b0d0-653f3fe89968, Connection=keep-alive, Content-Length=1557, X-Amz-Function-Error=Unhandled, Date=Mon, 11 Sep 2017 18:04:05 GMT, X-Amzn-Trace-Id=root=1-59b6d016-3a1ae0e3274a3e84e451bf13;sampled=0, Content-Type=application/json} Mon Sep 11 18:04:06 UTC 2017 : Method response body after transformations: {"errorMessage":"java.util.LinkedHashMap cannot be cast to com.morethanheroic.uppercase.domain.UppercaseRequest","errorType":"java.lang.ClassCastException","stackTrace":["com.morethanheroic.uppercase.UppercaseFunction.apply(UppercaseFunction.java:13)","org.springframework.cloud.function.support.FluxFunction.lambda$apply$0(FluxFunction.java:42)","reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107)","reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:90)","reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:159)","reactor.core.publisher.BlockingIterable$SubscriberIterator.onSubscribe(BlockingIterable.java:214)","reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)","reactor.core.publisher.FluxJust.subscribe(FluxJust.java:67)","reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63)","reactor.core.publi [TRUNCATED] Mon Sep 11 18:04:06 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-59b6d016-3a1ae0e3274a3e84e451bf13, Content-Type=application/json} Mon Sep 11 18:04:06 UTC 2017 : Successfully completed execution Mon Sep 11 18:04:06 UTC 2017 : Method completed with status: 200

我正在使用此JSON提供要反序列化到以下POJO的数据

I'm using this JSON to provide data to be de-serialized to the following POJO

 {"input":"richard was here"} 



public class UppercaseRequest {

    private String input;

    public String getInput() {
        return input;
    }

    public void setInput(final String input) {
        this.input = input;
    }
}

这里是处理程序

public class UppercaseFunctionHandler extends SpringBootRequestHandler<UppercaseRequest, UppercaseResponse> {
}

注意:取自以下示例项目 https://github.com/laxika/spring-cloud-function-aws-例子

NOTE: taken from sample project from https://github.com/laxika/spring-cloud-function-aws-example

示例中未记录-但我使用的Lambda入口点是:

It's not documented in the example - but the Lambda entry point that I used is:

com.morethanheroic.uppercase.handler.aws.UppercaseFunctionHandler :: handleRequest

com.morethanheroic.uppercase.handler.aws.UppercaseFunctionHandler::handleRequest

推荐答案

如果我以正确的方式指定处理程序(在本例中为com.morethanheroic.uppercase.handler.aws.UppercaseFunctionHandler),它对我有用:

It works for me if I specify the handler in the right way (com.morethanheroic.uppercase.handler.aws.UppercaseFunctionHandler in this case):

{
  "result": "FOO"
}

我认为错误是AWS控制台中的处理程序声明.您必须放置 class 的FQN(而不是方法引用).该方法不具有泛型类型信息,除非您重写它(您说过也可以,但不必要).

I think the mistake was the handler declaration in the AWS console. You have to put the FQN of the class (not a method reference). The method does not have the generic type information, unless you override it (which you said worked as well, but is unnecesasary).

OP进行了他自己的部署和测试功能的截屏视频( https://www.screencast. com/t/mNOLS1XhM ),除了处理程序名称外,几乎都是完美的.

The OP made a screencast of himself deploying and testing the function (https://www.screencast.com/t/mNOLS1XhM), which is almost all perfect except the handler name.

如果其他人想尝试,则代码在这里: https://github.com/laxika/spring-cloud-function-aws-example .

The code is here if anyone else wants to try it: https://github.com/laxika/spring-cloud-function-aws-example.

这篇关于AWS Lambda反序列化器/LinkedHashmap无法强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆