Skip to content
Advertisement

AWS SDK for java 1.7

I am trying to access a aws webservice here I have lambda expression, but my project is in java 7, So I want to convert this code to normal method.

   final Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> {
            System.out.println(in.getHttpResponse());
            return new ApiGatewayResponse(in.getHttpResponse());};

Advertisement

Answer

A lambda expression can be translated into either an anonymous class or a named class.

In your example, you need a class that implements the interface:

Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext>

If we look at that javadocs, we see that com.amazonaws.transform.Unmarshaller is defined as follows:

public interface Unmarshaller<T, R> {
    public T unmarshall(R in) throws Exception;
}

So we can create anonymous class + instance as follows:

Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller =
    new Unmarshaller<>() {
        public ApiGatewayResponse unmarshall(JsonUnmarshallerContext in) 
            throws Exception {
            return ...
        }
};

And the body of the unmarshall method is simply this:

            System.out.println(in.getHttpResponse());
            return new ApiGatewayResponse(in.getHttpResponse());


Note that there is something fishy about your example. According to the javadoc I am looking at ApiGatewayResponse is an abstract class, so we can’t new it. But the lambda you are translating (apparently) does.


Reference:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement