AWS Lambda 函数抛出 ClassNotFoundException - LambdaHandler。当前类路径:文件:/var/task/

Ati*_*med 5 java amazon-web-services spring-boot aws-lambda aws-api-gateway

我正在 APIGetway 之上作为 Lambda 部署的 Spring 启动应用程序 - 出现错误

 Class not found: com.myapp.app.LambdaHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.myapp.app.LambdaHandler. Current classpath: file:/var/task/
Run Code Online (Sandbox Code Playgroud)

创建 Jar 文件时,我看到有 3 个文件夹 - BOOT-INF、META-INF、org.jar 文件。

我的包位于 BOOT_INF -> classes -> com -> myapp -> app -> LambdaHandler 内

使用 serverless.yml 进行部署

从命令提示符 CLI 进行部署 - 无服务器部署

StreamLambdaHandler.java

public class StreamLambdaHandler implements RequestStreamHandler {  
  private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; 
    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
        } catch (Exception e) {
            // if we fail here. We re-throw the exception to force another cold start
            e.printStackTrace();
            throw new RuntimeException("Could not initialize Spring Boot Application", e);
        }
    }

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
            throws IOException {
        handler.proxyStream(inputStream, outputStream, context);
    } 
}
Run Code Online (Sandbox Code Playgroud)

应用程序.java

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
    /**
     * Create required HandlerMapping, to avoid several default HandlerMapping instances being created
     */
    @Bean
    public HandlerMapping handlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    /**
     * Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
     */
    @Bean
    public HandlerAdapter handlerAdapter() {
        return new RequestMappingHandlerAdapter();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器 -

@RestController
@EnableWebMvc
@Profile("lambda")
@RequestMapping("/app/v2")
public class ClientInformation {
    @Value("${clientDetails.userName}")
    private String userName;
    
    @Value("${clientDetails.userId}")
    private int userId;
    
    @Value("${clientDetails.userAddress}")
    private String userAddress;
    
    @GetMapping("/")
    public Map<String, Object> getDetails()  {  
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("message", "App is working fine !!!");
        return rtn;
    }
    
    @GetMapping("/information")
    public Map<String, Object> getInformation()  {  
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("name", userName);
        rtn.put("id", userId);
        return rtn;
    }
    
    @GetMapping("/ping")
    public Map<String, Object> getD() {
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("name", "Ping is happening !!!");
        rtn.put("status", "200");
        return rtn;
    }
}
Run Code Online (Sandbox Code Playgroud)

无服务器.yml -

service: myapplication-api
provider:
  name: aws
  runtime: java11
  memorySize: 512
  timeout: 15
 profile: default
  stage: production
package:
  artifact: target/myapp-api-0.0.1-SNAPSHOT-exec.jar

functions:
  catchAllAny:
    handler: index.catchAllAny
    events:
      - httpApi: '*'
  catchAllMethod:
    handler: handler.catchAllMethod
    events:
      - httpApi:
          method: '*'
          path: /any/method
Run Code Online (Sandbox Code Playgroud)

pom.xml

我可以知道我做错了什么吗?-

 Class not found: com.myapp.app.LambdaHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.myapp.app.LambdaHandler. Current classpath: file:/var/task/
Run Code Online (Sandbox Code Playgroud)

小智 0

您需要配置 serverless.yml 以将 StreamLambdaHandler 的完整包指定为处理程序属性。

例如,如果该类位于 com.myapp 包中。您需要指定 com.myapp.StreamLambdaHandler

您当前指定的 com.myapp.app.LambdaHandler 是一个不存在的类。