java 1.7 的 AWS 开发工具包

AWS SDK for java 1.7

我想在这里访问 aws webservice 我有 lambda 表达式,但我的项目在 java 7,所以我想将此代码转换为普通方法。

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

lambda 表达式可以转换为匿名 class 或命名 class。

在您的示例中,您需要 class 实现接口:

Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext>

如果我们查看 javadocs,我们会看到 com.amazonaws.transform.Unmarshaller 定义如下:

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

所以我们可以创建匿名 class + 实例如下:

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

unmarshall 方法的主体是这样的:

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

请注意,您的示例有些可疑。根据javadoc我看ApiGatewayResponse是抽象的class,所以不能new。但是您正在翻译的 lambda(显然)确实如此。


参考: