0
votes

I have a function on AWS Lambda written in Java that can currently only accept text event data, not JSON.

Whenever I go in the test console, if my test event has a payload of "{}", the function runs, however if it's a json, {} it does not.

If I run it with any of the triggers, such as S3, CloudWatch Events or with a API Gateway, they always send out a json.

Is there by any chance a possibility to run those triggers and make them send a string payload instead of a json payload?

1

1 Answers

0
votes

To accept JSON as input, you need to update your RequestHandler<JSONObject, String> and

public class Hello implements RequestHandler<JSONObject, String>{
    public String myHandler(JSONObject jsonObj, Context context) {
       // add your processing logic.
       return "done!";
    }
}

Alternatively, you can define a POJO with the attributes of your json payload like

class Payload {
  //fields
  //setter & getter
}

And use this Payload type in your RequestHandler as

public class Hello implements RequestHandler<Payload, String>{
    public String myHandler(Payload payload, Context context) {
       // add your processing logic.
       return "done!";
    }
}

Here String is your output data type. If its different in your case, you need to change that as well.