简体   繁体   中英

why does this AWS Lambda code in Java return "internal server error" when invoked via an API gateway?

From my understanding, if you return the following json string: {"headers":{"Content-Type":"application/json"},"body":"hello world","statusCode":200}

then when visiting the page https://....eu-west-2.amazonaws.com/prod/ , you should see the phrase "hello world" written. Instead, I get {"message": "Internal server error"}

Below is the JAVA code I am using to return the above json string:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.json.simple.JSONObject;
import java.util.Map;

public class Handler implements RequestHandler<Map<String, String>, JSONObject>{
    @Override
    public JSONObject handleRequest(Map<String, String> stringStringMap, Context context) {

        JSONObject obj = new JSONObject();
        JSONObject obj2 = new JSONObject();
        obj2.put("Content-Type", "application/json");
        obj.put("statusCode", 200);
        obj.put("headers", obj2);
        obj.put("body", "hello world");
        return obj;
    }
}

What am I doing wrong? It works fine on the console when I test the function, but when I go to the page of the API it gives me an internal server error.

The error message you posted in the comment has already explained what the problem is. Basically, lambda handler is not able to convert the input object to String . You can solve this by changing Map<String, String> to Map<String, Object> or general Object .

Also, as @david pointed out in the comment, you should return a JSON String instead of a JSONObject . So the fully working code could be something like this:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.json.simple.JSONObject;
import java.util.Map;

public class Handler implements RequestHandler<Map<String, Object>, String>{
    @Override
    public String handleRequest(Map<String, Object> stringStringMap, Context context) {

        JSONObject obj = new JSONObject();
        JSONObject obj2 = new JSONObject();
        obj2.put("Content-Type", "application/json");
        obj.put("statusCode", 200);
        obj.put("headers", obj2);
        obj.put("body", "hello world");
        return obj.toString();
    }
}

For anyone interested, the following code worked fine for me:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import org.json.simple.JSONObject;

import java.io.*;

public class Handler implements RequestStreamHandler{
    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, 
                                      Context context) throws IOException {

        JSONObject obj = new JSONObject();
        JSONObject obj2 = new JSONObject();
        obj2.put("Content-Type", "application/json");
        obj.put("statusCode", 200);
        obj.put("headers", obj2);
        obj.put("body", "hello world");

        OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
        writer.write(obj.toString());
        writer.close();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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