[英]AWS Lambda - Spring @Autowire
我在 Spring Boot 中使用 RestController 開發了一個項目,它沒有錯誤地工作。
現在我使用 AWS lambda,但是當我嘗試將 Spring bean 注入 lambda 處理程序時,我得到了 null 指針異常:
public class HelloWorldHandler implements RequestHandler<Map<String, Object>, Object> {
@Autowired
private IUserService userservice;
public Object handleRequest(Map<String, Object> input, final Context context) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
User testUser = new User();
testUser.setId("10");
testUser.setLastname("test_lastname");
testUser.setMail("test@al.com");
userservice.addUser(testUser);
return new GatewayResponse("success", headers, 200);
}
}
例外:
{
"errorMessage": "java.lang.NullPointerException",
"errorType": "java.lang.NullPointerException",
"stackTrace": [
"HelloWorldHandler.handleRequest(HelloWorldHandler.java:30)",
"HelloWorldHandler.handleRequest(HelloWorldHandler.java:21)"
]
}
HelloWorldHandler 不是 Spring 組件,因此您不能只向該 class 注入一個 bean。甚至沒有加載 Spring 上下文。 所以它不能工作:)
您可以使用 Spring Cloud Function 運行上下文並訪問所有 Spring 功能,包括“Autowired”。
或者你可以試試這個: https://github.com/MelonProjectCom/lambda-http-router
易於使用並允許在單個 lambda 中運行多個處理程序:
@PathPostMapping("/example/test")
public String example(@Body String body) {
return "Hello World! - body: " + body;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.