AWS Can not deserialize instance of java.lang.String out of START_OBJECT

Sat, Jul 18, 2020

Read in 3 minutes

Introduction :

In this article we will see how to write a simple lamda function and test that and also how to solve the error which occurs when you start learning lamda.

Steps :

1. Create a maven project in eclipse 
2. Add the dependencies 
3. Write the code for lamda function 
4. Upload the lamda code into AWS
5. Test the lamda function

Create a maven Project in eclipse : File -> new maven project

AWSServices

AWSServices

If you opened the project structure ,you can find App.java ,pom.xml and files .

Add the dependencies :

In the POM.xml add the below dependies

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-events -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-events</artifactId>
    <version>3.0.0</version>
</dependency>

Write the code for lamda function

When you trying to write a lamda function in a class that class needs to implements the interface RequestHandler<> and always you have to pass object as a input (RequestHandler<Object, Object>) .

For example if you try to pass <String ,object > you will get the error “AWS Can not deserialize instance of java.lang.String out of START_OBJECT”.

And the interface RequestHandler is having a method ,

public O handleRequest(I input, Context context);

Inside this method you can write your logic for your lamda function .

Here ,I am planning to generate a random number and I getting input object and I am just printing that object.I am returning the random number generated as object by using Input.java

package com.theprogrammerguide.lamdademo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class App implements RequestHandler<Object, Object> {
public Object handleRequest(Object obj, Context context) {
Double temp = Math.random() * 100;

String random = Double.toString(temp);

System.out.println("input object received is" + obj.toString());
System.out.println("generated random number is" + random);
Input input =new Input();
input.setRandomNumber(random);

return input;
}

}

Right click the project and select maven clean ,maven install and generate resources .Now the jar file might have been created in the “target " folder.you have to upload the jar into AWS lamda.

Upload the code ( or jar) into AWS

Login your AWS account .If you don’t have one ,get the introduction about AWS https://www.theprogrammerguide.com/post/aws-introduction/ and you can login for the free tier account.

After login click services and select lamda and click on "create function" .

AWSServices

In the create function screen enter function name ,select java runtime what you are using in your eclipse .I am using java 8 so ,I selected that.

AWSServices

AWSServices

After click on “Create function " you might have landed into designer page.

In the function code section ,upload your jar and in the basic settings section ,you have to update the packagename.ClassName:: methodName.

AWSServices

And leave all other settings with the default entries .

Now your lamda function is ready to test.

Test the Lamda function :

To test the lamda function you have to configure the event. Click on test and As I am not using the input for my logic ,I just take the default input event.

AWSServices

Finally , you will geth the below screen of exceution result.

AWSServices