Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS, Lambda, Java, POJO, custom json property name

Making first attempts with Lambda. Created code, deployed, test works, but:

public String handleRequest(MyType inObj, Context context) {
    // logging inObj here
}

POJO class

public class MyType {
    String prop;
    String otherProp;
}

when invoking, I give following payload:

{ "prop": "val1", "other_prop": "val2" }

As you can see, I want to give json in snake_case. When lambda logs, I can see that

inObj.prop == "val1"

inObj.otherProp == "null".

When I change JSON from snake_case to camelCase, it is deserialized properly and otherProp == "val2". I tried adding @JsonProperty("other_prop") to the field, adding getter and setter (in camelCase) and adding @JsonProperty to those (random guess), but nothing changed.

Q: how can I describe MyType class so that it is properly deserialized from snake_case to camelCase by AWS Lambda?

like image 369
glebsts Avatar asked Jul 08 '16 19:07

glebsts


1 Answers

See note in http://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html

Note

You shouldn't rely on any other features of serialization frameworks such as annotations. If you need to customize the serialization behavior, you can use the raw byte stream to use your own serialization.

So you need to serialize the objects from the inputstream to make use of annotations.

http://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-stream.html

package example;

import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context; 

public class Hello implements RequestStreamHandler {
    public static void handler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        // TODO Serialize Object from inputStream
    }
}
like image 55
SimonSikstrom Avatar answered Nov 03 '22 17:11

SimonSikstrom