0

this is my code.

package com.example.lambda.dynamodbdemo;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class PuttingPersonHandler implements RequestHandler<Person, String> {
    private DynamoDB dynamoDb;
    private String TABLE_NAME = "People";
    private String REGION = "ap-northeast-2";

    @Override
    public String handleRequest(Person input, Context context) {
        this.initDynamoDbClient();

        putData(input);
        return "Saved Successfully!!";
    }

    private void initDynamoDbClient() {
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                .withRegion(REGION).build();
         this.dynamoDb = new DynamoDB(client);
    }

    private PutItemOutcome putData(Person person) 
              throws ConditionalCheckFailedException {
                return this.dynamoDb.getTable(TABLE_NAME)
                  .putItem(
                    new PutItemSpec().withItem(new Item()
                            .withPrimaryKey("id",person.id)
                            .withString("firstName", person.firstName)
                            .withString("lastName", person.lastName)));
            }
}

class Person {
    int id;
    String firstName;
    String lastName;
}

when I trying to run function on AWS Lambda with this json massage,

{
  "id": "1",
  "firstName": "Min",
  "lastName": "Heo"
}

and I get this error

Input value must not be null: java.lang.IllegalArgumentException java.lang.IllegalArgumentException: Input value must not be null at com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.rejectNullValue(InternalUtils.java:595) at com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.checkInvalidAttribute(InternalUtils.java:620) at com.amazonaws.services.dynamodbv2.document.Item.withString(Item.java:108) at com.example.lambda.dynamodbdemo.PuttingPersonHandler.putData(PuttingPersonHandler.java:38) at com.example.lambda.dynamodbdemo.PuttingPersonHandler.handleRequest(PuttingPersonHandler.java:22) at com.example.lambda.dynamodbdemo.PuttingPersonHandler.handleRequest(PuttingPersonHandler.java:1)

Is there any way to solve this error?

1 Answer 1

1

From https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html: :

The get and set methods are required in order for the POJOs to work with AWS Lambda's built in JSON serializer

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.