Home > Software design >  AWS lambda works but not Gateway
AWS lambda works but not Gateway

Time:10-28

On the beginning I would like to mention that I just start learning AWS tools.

I have two request handlers created with Java. Both of them have the same handleRequest method which looks like one below

public class SecondHandler implements RequestHandler<JSONObject, List<Candidate>> {
 

    @Override
    public List<Object> handleRequest(JSONObject request, Context context) {
        List<Object> objectList = new ArrayList<>();
        System.out.println(request   " <<");
         String isActive = String.valueOf(request.getString("isActive"));        
        
        if(!isActive) return objectList;

        for(Object obj : objectList) {
          // here will go logic
        }

      
        return objectList;
    }
}

First method is used to return some list of object without sending any arguments to it. And it work fine when calling it with AWS lambda, and when using AWS Gateways.

Second method I am trying to call with sending JSONObject with one filed like

{
  "isActive" : "yes"
}

But when I do the test I am getting error "JSONObject[\"isActive\"] not found.". And I am sure that this is related to my JSONObject request argument, because when I am changing its type to Map<String, String> it works fine, I am getting what I want when using AWS Lambda. But with Map in place when I try to call this with AWS Gateway I am getting

{ "message" : "Internal server error" }

When checking the logs I can see there is problem with parsing. So basically AWS Gateway can not parse Map<String, String>.

Could someone tell me, what I do not understand about lambda and gateways? Why those tools have such problem with parsing simple JSON? What I should do?

PS. I also had the issue with parsing on the first method, when using Map<String, String> that is why I decided to use JSONObject, and that solved the issue for first method.

CodePudding user response:

If you can't use Map<String, Object>, implement the RequestStreamHandler interface.

It doesn't require a data type and will provide you with an InputStream. You can then convert the stream to any compatible data type, including a JSONObject.

Try this:

public class SecondHandler implements RequestStreamHandler {

    @Override
    public void handleRequest(InputStream input, OutputStream output, Context context) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        JsonElement eventElement = new JsonParser().parse(reader);
        JsonObject event = (JsonObject) eventElement;
        ...
    }
}

CodePudding user response:

Thanks @Ermiya Eskandary for help. You help figured out that my handler should look like one below.


import org.json.JSONObject;
import org.json.JSONTokener;

public class SecondHandler {

  public void handleRequest(InputStream input, Context context) {
    BufferedReader bufferedReader = new BufferedReader(new 
    InputStreamReader(input));
    JSONTokener tokener = new JSONTokener(bufferedReader);
    JSONObject json = new JSONObject(tokener);

    // and in here I can use my json 
  }
}

I was trying use your method, but new JsonPArser().parse(reader); not sure what library you were using there. I failed twice with Gson and Jackson. But your suggestion help to come up with proper solution. thanks again.

  • Related