DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Low Code Approach for Building a Serverless REST API
  • Deploying Java Serverless Functions as AWS Lambda
  • Getting started with Java Serverless Functions using Quarkus and AWS Lambda
  • Building Scalable Data Lake Using AWS

Trending

  • Rust, WASM, and Edge: Next-Level Performance
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  • How to Convert XLS to XLSX in Java
  • Data Quality: A Novel Perspective for 2025
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. AWS Lambda Errors With Java Lambdas and Serverless Framework

AWS Lambda Errors With Java Lambdas and Serverless Framework

Make your lambdas work right with Lambda.

By 
Kevin Hooke user avatar
Kevin Hooke
·
Jan. 23, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Using a basic Serverless framework event config like this:

Properties files
 




x


 
1
functions:
2
  hello:
3
    handler: kh.javalambda.HelloLambda::myHandler
4
    events:
5
      - http:
6
        path: hello
7
        method: get



...will create an API Gateway config with the Lambda Proxy feature enabled.

This sends the request as a JSON object to the Lambda. This includes HTTP headers, queryStringParameters, pathParameters and the request body. The Lambda is expected to have an appropriate parameter type for the incoming JSON payload otherwise you'll get a Jackson parsing error like this:

Plain Text
 




xxxxxxxxxx
1


 
1
An error occurred during JSON parsing: java.lang.RuntimeException
2
 java.lang.RuntimeException: An error occurred during JSON parsing
3
 Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
4
  at [Source: lambdainternal.util.NativeMemoryAsInputStream@1bce4f0a; line: 1, column: 1]
5
 Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
6
  at [Source: lambdainternal.util.NativeMemoryAsInputStream@1bce4f0a; line: 1, column: 1



Use a Map<String, Object> as shown in the docs and the incoming request JSON will get passed in as this map.

Even if you are calling the Lambda with a GET request and not intending to POST or PUT a request body, you still need to have the Map<String, Object> parameter to receive the JSON event containing all the headers and params passed from API gateway.

Return Type

If you return a regular String or anything other than the expected response payload, from your Lambda will cause API Gateway to fail parsing the expected JSON response:

Plain Text
 




xxxxxxxxxx
1


 
1
Wed Jan 13 07:34:32 UTC 2021 : Endpoint response body before transformations: "HelloLambda: hello null" Wed Jan 13 07:34:32 UTC 2021 : Execution failed due to configuration error: Malformed Lambda proxy response Wed Jan 13 07:34:32 UTC 2021 : Method completed with status: 502



With the API Gateway Lambda Proxy feature enabled, API Gateway expects response payloads from your Lambda to match the format shown here in the docs. Serverless framework generates a class called APIGatewayResponse that matAches the expected format. Either create your own POJO that matches the expected return format or use the generated APIGatewayResponse class.

The generated sample Handler class shows this in use:

Java
 




xxxxxxxxxx
1


 
1
return ApiGatewayResponse.builder()
2
  .setStatusCode(200)
3
  .setObjectBody(responseBody)
4
  .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))
5
  .build();



Serverless Framework AWS AWS Lambda Framework Java (programming language)

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Low Code Approach for Building a Serverless REST API
  • Deploying Java Serverless Functions as AWS Lambda
  • Getting started with Java Serverless Functions using Quarkus and AWS Lambda
  • Building Scalable Data Lake Using AWS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!