AWS Lambda反序列化程序/ LinkedHashmap无法投射

时间:2017-09-11 18:27:47

标签: java aws-lambda aws-api-gateway

我是Java / AWS等的新手,而AWS lambda我们没有反序列化我认为是一个微不足道的例子。

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

  

请求测试请求的执行日志   星期一9月11日18:04:06 UTC 2017:开始执行请求:test-invoke-request   星期一9月11日18:04:06 UTC 2017:HTTP方法:POST,资源路径:/   星期一9月11日18:04:06 UTC 2017:方法请求路径:{}   星期一9月11日18:04:06 UTC 2017:方法请求查询字符串:{}   星期一9月11日18:04:06 UTC 2017:方法请求标题:{Content-Type = application / json}   星期一9月11日18:04:06 UTC 2017:转换前的方法请求体:{"输入":" richard在这里"}   星期一9月11日18:04:06 UTC 2017:终点请求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:端点请求标头:{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-西2:003374398906:8fjgcr51va /空/ POST /,接受=应用/ JSON,用户代理= AmazonAPIGateway_8fjgcr51va,X-AMZ-安全令牌= XXXXXXX ////////// wEaDM3BrDjbuGSwMFT5lCK3AyBVM7duW9tzEe / bcWqWiYNIxkpMjTdDZaW3U32asJ4qvntUsRpjfSIhLWAds74XhzHbI7GzXMuV3zQYCIiYRX0ZL1s524J7mETGFN4OredlGln7CCEs3WR417UaPB4XLh6E0v71Srpg4a1kG7KB6426gN9CvXR0mmYbwpl9qtp9bovLgIoVnjYbx5j7qd7Fa3U4wQOqcHonlKtN / uDXMFW + vfHOJyu3gQUyvoVpMuXrQSF / gptEXG + l0v4 + v1exq67sp8G5d8h1kAQTN Qep8Q19kyOi9hWbNDyU7FzXWvfSRX7f9n6NGIuZ6LYIF3g2kPFY [截止]   星期一9月11日18:04:06 UTC 2017:转换后的端点请求正文:{"输入":" richard在这里"}   星期一9月11日18:04:06 UTC 2017:向https://lambda.us-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-2:003374398906:function:uppertolowercase/invocations发送请求   星期一9月11日18:04:06 UTC 2017:收到回复。集成延迟:73毫秒   星期一9月11日18:04:06 UTC 2017:转换前的端点响应体:{" errorMessage":" java.util.LinkedHashMap无法转换为com.morethanheroic.uppercase.domain.UppercaseRequest&# 34;," ERRORTYPE":" java.lang.ClassCastException""堆栈跟踪":[" com.morethanheroic.uppercase.UppercaseFunction.apply(UppercaseFunction的.java:13)"" org.springframework.cloud.function.support.FluxFunction.lambda $ $施加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)&#34 ;, " reactor.core.publisher.FluxMapFuseable $ MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)"&#3 4; reactor.core.publisher.FluxJust.subscribe(FluxJust.java:67)"" reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63)"&# 34; reactor.core.pu [TRUNCATED]   星期一9月11日18:04:06 UTC 2017:终点响应标题:{x-amzn-Remapped-Content-Length = 0,x-amzn-RequestId = 9a0ca35b-971b-11e7-b0d0-653f3fe89968,Connection = keep-alive, Content-Length = 1557,X-Amz-Function-Error =未处理,Date = Mon,2017年9月11日18:04:05 GMT,X-Amzn-Trace-Id = root = 1-59b6d016-3a1ae0e3274a3e84e451bf13; samples = 0,内容类型=应用/ JSON}   星期一9月11日18:04:06 UTC 2017:转换后的方法响应体:{" errorMessage":" java.util.LinkedHashMap无法转换为com.morethanheroic.uppercase.domain.UppercaseRequest&# 34;," ERRORTYPE":" java.lang.ClassCastException""堆栈跟踪":[" com.morethanheroic.uppercase.UppercaseFunction.apply(UppercaseFunction的.java:13)"" org.springframework.cloud.function.support.FluxFunction.lambda $ $施加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)&#34 ;, " reactor.core.publisher.FluxMapFuseable $ MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)"" R eactor.core.publisher.FluxJust.subscribe(FluxJust.java:67)"" reactor.core.publisher.FluxMapFuseable.subscribe(FluxMapFuseable.java:63)"" reactor.core.publi [截止]   星期一9月11日18:04:06 UTC 2017:方法响应标题:{X-Amzn-Trace-Id = samples = 0; root = 1-59b6d016-3a1ae0e3274a3e84e451bf13,Content-Type = application / json}   星期一9月11日18:04:06 UTC 2017:成功完成执行   星期一9月11日18:04:06 UTC 2017:方法已完成,状态:200

我使用此JSON提供要反序列化到以下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-example

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

  

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

1 个答案:

答案 0 :(得分:3)

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

{
  "result": "FOO"
}

我认为错误是AWS控制台中的处理程序声明。您必须放置的FQN(不是方法引用)。该方法没有泛型类型信息,除非你覆盖它(你说它也起作用,但是不必要)。

OP制作了一个自己部署和测试函数(https://www.screencast.com/t/mNOLS1XhM)的截屏视频,除了处理程序名称外几乎都是完美的。

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