简体   繁体   English

AWS lambda 有效,但网关无效

[英]AWS lambda works but not Gateway

On the beginning I would like to mention that I just start learning AWS tools.一开始我想提一下,我刚刚开始学习 AWS 工具。

I have two request handlers created with Java.我有两个用 Java 创建的请求处理程序。 Both of them have the same handleRequest method which looks like one below它们都具有相同的handleRequest方法,如下所示

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.使用 AWS lambda 调用它以及使用 AWS 网关时,它都能正常工作。

Second method I am trying to call with sending JSONObject with one filed like第二种方法我试图通过发送 JSONObject 来调用

{
  "isActive" : "yes"
}

But when I do the test I am getting error "JSONObject[\\"isActive\\"] not found."但是当我进行测试时,我收到错误"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.而且我确信这与我的JSONObject request参数有关,因为当我将其类型更改为Map<String, String>它工作正常,使用 AWS Lambda 时我得到了我想要的东西。 But with Map in place when I try to call this with AWS Gateway I am getting但是当我尝试使用 AWS Gateway 调用它时,我得到了Map

{ "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> .所以基本上 AWS Gateway 无法解析Map<String, String>

Could someone tell me, what I do not understand about lambda and gateways?有人能告诉我,我不了解 lambda 和网关吗? Why those tools have such problem with parsing simple JSON?为什么这些工具在解析简单的 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.我在解析第一种方法时也遇到了问题,当使用Map<String, String>这就是我决定使用JSONObject ,这解决了第一种方法的问题。

If you can't use Map<String, Object> , implement the RequestStreamHandler interface.如果您不能使用Map<String, Object> ,请实现RequestStreamHandler接口。

It doesn't require a data type and will provide you with an InputStream .它不需要数据类型,并且会为您提供InputStream You can then convert the stream to any compatible data type, including a JSONObject .然后,您可以将流转换为任何兼容的数据类型,包括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;
        ...
    }
}

Thanks @Ermiya Eskandary for help.感谢@Ermiya Eskandary 的帮助。 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(is));
    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);我正在尝试使用您的方法,但是new JsonPArser().parse(reader); not sure what library you were using there.不确定你在那里使用的是什么库。 I failed twice with Gson and Jackson.我用 Gson 和 Jackson 失败了两次。 But your suggestion help to come up with proper solution.但是您的建议有助于提出适当的解决方案。 thanks again.再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM