Enterprise Java

AWS Lambda function with Java Pojo as Input Output Example

In the previous tutorial, we saw  How to Create AWS Lambda function with Java and we passed String as input and also returned String as Output.I will recommend to go through that tutorial first, if you are creating lambda function for the first time.

In this tutorial, we will see, how we can pass Java Plain old Java object(POJO) as input and also return it as Output.

Here we will be implementing RequestHandler interface.

package com.blogspot.javasolutionsguide;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.blogspot.javasolutionsguide.input.Input;
import com.blogspot.javasolutionsguide.output.Output;

public class HelloWorldWithPojoInputOutput implements RequestHandler{

 @Override
 public Output handleRequest(Input input, Context context) {
  String message = String.format("Hello %s%s.", input.getName() , " " + "from" + context.getFunctionName());
  return new Output(message);
 }

 
}

And here is our Input and Output classes.

package com.blogspot.javasolutionsguide.input;


public class Input {
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

package com.blogspot.javasolutionsguide.output;


public class Output {
 
 private String message;
 
 public Output(String message) {
  this.message = message;
 }

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }
 
}

For uploading the code to AWS console, please follow steps from my previous tutorial.

Once you have uploaded your jar in AWS lambda console, click on “Select a Test event” and then “configure test events”.

Enter event name as “HelloWorldWithPojoInputOutput” and replace following

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

with following :

{
  "name" : "Gaurav Bhardwaj"
}

and click on Create button.

Now click on Test button and you should see your lambda function executed successfully with message “Hello Gaurav Bhardwaj from HelloWorld”,which is the output returned by our lambda function.

You can find all code of this tutorial in GitHub

Summary

So in this tutorial, we saw how we can pass POJO to lambda function and also we can return POJO from a lambda function.

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: AWS Lambda function with Java Pojo as Input Output Example

Opinions expressed by Java Code Geeks contributors are their own.

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button