尝试从aws lambda处理函数调用SDN时java.lang.NullPointerException

时间:2016-09-26 07:06:13

标签: java amazon-web-services neo4j spring-data-neo4j aws-lambda

我有一个处理函数,我在其中调用我的spring数据存储库接口。代码在我的main方法或jar中工作正常,但是当我在s3中上传jar并从aws lambda调用我调用存储库方法时抛出NullPointerException。下面是我的lambda处理程序方法;

public class LambdaRestFunction implements RequestHandler<String, Object> {

    @Autowired
    UserRepository userRepository;

    @Override
    public String handleRequest(String uname, Context context) {
        String time = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime());

        context.getLogger().log("Lambda call started: " + time);

        List<User> users = new ArrayList<>();
        context.getLogger().log("Array initialized.");
        **users = userRepository.findByName(uname);** // <-- NullPointerException thrown here
        context.getLogger().log("Method call over..");
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String json = "[]";
        try {
            json = ow.writeValueAsString(users);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            context.getLogger().log("Error from application : " + e.getMessage());
        }

        context.getLogger().log("Lambda function completed: " + time);
        return json;
    }

}

我的存储库界面是;

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends GraphRepository<User> { 
    @Query("MATCH (u:User) WHERE u.id =({id}) RETURN u")
    User findUser(@Param("id") Long id);    
}

和域类是;

@NodeEntity
public class User {

    @GraphId Long id;
    private String name;
    private String dob;
    private String cc;

    public User(String name, String dob, String cc) {
        this.name = name;
        this.dob = dob;
        this.cc = cc;
    }

    public User() {
    }

    public String getName() {
        return name;
    }
...

请在下面找到跟踪日志:

java.lang.NullPointerException
at com.test.lambda.LambdaRestFunction.handleRequest(LambdaRestFunction.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:439)
at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:370)
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:972)
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:235)
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:60)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94) 

如果有人能帮助我找到异常的原因,我将非常感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

Lambda不支持注释,因此@Autowired将无法获得所需的效果。

https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html,请参阅&#34;处理程序输入/输出:POJO类型&#34;下的说明。