使用AWS Sdk调用AWS Lambda时的NoSuchMethod

时间:2017-04-14 04:22:45

标签: java amazon-web-services aws-lambda aws-sdk aws-java-sdk

我创造了&在API网关中部署了一个简单的GET API,这里是ARN,在此功能上没有任何身份验证,我可以在浏览器上调用它

arn:aws:lambda:ap-southeast-1:XXXXXXXXXXXXXX:function:La

可以使用浏览器浏览的公共网址是:

https://xxxxxxxxx.execute-api.ap-southeast-1.amazonaws.com/v1/lambda/geta

我正在使用Spring启动项目和以下代码来调用API(关注this Doc

作为lambda服务的接口

package com.xxxxxxx.services.interfaces;

import com.amazonaws.services.lambda.invoke.LambdaFunction;

public interface ILambdaGetBalance {

    @LambdaFunction(functionName="La")
        String getA();
    }

使用该接口调用lambda函数的服务

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.xxxxxxxx.services.interfaces.ILambdaGetBalance;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.invoke.LambdaInvokerFactory;

@Service
public class LambdaService {

    @Value("${aws.access-key}")
    private String accessKey; 

    @Value("${aws.secret-key}")
    private String secretKey;

    @Value("${aws.lambda.region-name}") // this is ap-southeast-1
    private String regionName;

    public void test() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey,
                secretKey); 
        AWSLambda client = AWSLambdaClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(regionName)
                .build();

        final ILambdaGetBalance getBalance = LambdaInvokerFactory.builder()
                .lambdaClient(client)
                .build(ILambdaGetBalance.class);

        getBalance.getA();
    }
}

调用getA函数后,系统将通过以下异常:

java.lang.NoSuchMethodError: com.amazonaws.services.lambda.AWSLambdaClient.beforeClientExecution(Lcom/amazonaws/AmazonWebServiceRequest;)Lcom/amazonaws/AmazonWebServiceRequest;

知道为什么会这样吗?我错过了什么?

1 个答案:

答案 0 :(得分:0)

您的aws-java-sdk-lambdaaws-java-sdk-core模块看起来可能有不兼容的版本。您如何解决项目的依赖关系? beforeClientExecution方法已添加到AmazonWebServiceClient版本1.11.106中的aws-java-sdk-core基类 - 请参阅此处:https://github.com/aws/aws-sdk-java/blame/master/aws-java-sdk-core/src/main/java/com/amazonaws/AmazonWebServiceClient.java#L590