AWS SDK for java 1.7
我试图访问aws Web服务,这里有lambda表达式,但我的项目是java 7,所以我想将此代码转换为普通方法AWS SDK for java 1.7,java,amazon-web-services,sdk,Java,Amazon Web Services,Sdk,我试图访问aws Web服务,这里有lambda表达式,但我的项目是java 7,所以我想将此代码转换为普通方法 final Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> { System.out.println(in.getHttpResponse()); return new ApiGatewa
final Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> {
System.out.println(in.getHttpResponse());
return new ApiGatewayResponse(in.getHttpResponse());};
final Unmarshaller responseUnmarshaller=in->{
System.out.println(in.getHttpResponse());
返回新的ApiGatewayResponse(在.getHttpResponse());};
lambda表达式可以转换为匿名类或命名类
在您的示例中,您需要一个实现接口的类:
Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext>
所以我们可以创建匿名类+实例,如下所示:
public interface Unmarshaller<T, R> {
public T unmarshall(R in) throws Exception;
}
Unmarshaller<ApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller =
new Unmarshaller<>() {
public ApiGatewayResponse unmarshall(JsonUnmarshallerContext in)
throws Exception {
return ...
}
};
请注意,您的示例有些可疑之处。根据我正在查看的
ApiGatewayResponse
是一个抽象类,因此我们不能new
它。但是你正在翻译的lambda(显然)是这样的
参考:
JSONumarshallerContext
,但这是一个抽象类,所以我创建了一个自定义类来扩展JSONumarshallerContext。但它仍然返回空对象JSONumarshallerInstitanter JSONumarshallerContext=new JSONumarshallerInstitanter();最终解组器responseUnmarshaller=(解组器)新的ApiGatewayResponse(jsonUnmarshallerContext.getHttpResponse());而且。。。它有效吗?我的观点是,如果你让别人为你写代码,你不会学会如何为自己写代码。如果您想让资源了解lambdas,请从Oracle Java教程开始:@StephenC不,它不起作用,我在这里遗漏了什么吗?非常感谢您的回答,它起作用了。我会调查医生的。
System.out.println(in.getHttpResponse());
return new ApiGatewayResponse(in.getHttpResponse());