0

I have a lambda function that accepts a String as an input parameter. When running the lambda function I get the following error: Can not deserialize instance of java.lang.String out of START_OBJECT token\n

This is what my code too call it looks like:

InvokeRequest request = new InvokeRequest();
final String payload = "";
request.withFunctionName(FUNCTION_NAME).withPayload((String) null);
InvokeResult invokeResult = lambdaClient.invoke(request);
Assert.assertEquals(new String (invokeResult.getPayload().array(), "UTF-8"), "Success");

And this is what my handler looks like:

public String handleRequest(String s, Context context) {}

Now the contents of the string don't matter, it could be null it could be anything. I don't use the input. The obvious solution is to remove it, but because of an annotation generator i'm using I can't do that. I've tried a ByteBuffer input, String, empty String, JSON String {\"s\":\"s\"} but nothing seems to work. I believe I need to pass in a string (i.e no {}). But since I'm using InvokeRequest I don't believe I can do that. Any suggestions would be greatly appreciated.

2
  • Try using the solution as provided here. Hope it'll help stackoverflow.com/questions/41616806/… Commented Aug 20, 2019 at 11:45
  • Thanks for the link! I actually followed that same tutorial and it didn't work. I appreciate it though! Commented Aug 20, 2019 at 14:18

1 Answer 1

0

It works by passing a JSON valid String.

String payload = "{ \"subject\" : \"content\"}";
request.withFunctionName(functionName)           
                .withPayload(payload);
  

At the receiving end you have to map it from Object to String again if that's what you want. Here I used Jackson ObjectMapper for this.

ObjectMapper objectMapper = new ObjectMapper();
        try {
            String payload = objectMapper.writeValueAsString(input);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
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.