3
votes

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

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

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

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;
    }
}

Here is the handler

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

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

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

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

1
If that is the JSON, and that is the target, then AWS knows how to do the conversion. So I expect you have omitted some key information. You didn't say anything about the handler or the way it was declared to AWS, for instance.Dave Syer
Update the question with the handlercodeputer
So you are using spring cloud function. Does the sample project work, or is that the code you are running (unclear from context so far)?Dave Syer
How did you deploy to Lambda?Dave Syer
I upload zip to aws directly... wierd part is that i had this issue, then it went away and i couldnt figure out why, uploaded again and now its failing. Tests confirm that json will work with class, but in aws it wont.codeputer

1 Answers

3
votes

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

{
  "result": "FOO"
}

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).

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.

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