gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:08:15 26 4
gpt4 key购买 nike

我有一个处理程序函数,在其中调用我的 Spring 数据存储库接口(interface)。该代码在我的 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)

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

最佳答案

Lambda不支持注解,因此@Autowired不会达到预期的效果。

https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-req-resp.html ,请参阅“处理程序输入/输出:POJO 类型”下的注释。

关于尝试从 aws lambda 处理函数调用 SDN 时出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39696637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com