[英]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 (顯然)確實如此。
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.