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.
JavaScript
x
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:
JavaScript
Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext>
If we look at that javadocs, we see that com.amazonaws.transform.Unmarshaller
is defined as follows:
JavaScript
public interface Unmarshaller<T, R> {
public T unmarshall(R in) throws Exception;
}
So we can create anonymous class + instance as follows:
JavaScript
Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller =
new Unmarshaller<>() {
public ApiGatewayResponse unmarshall(JsonUnmarshallerContext in)
throws Exception {
return
}
};
And the body of the unmarshall
method is simply this:
JavaScript
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: