AWS Lambda函数抛出ClassNotFoundException-LambdaHandler.当前类路径:fil



我正在APIGetway顶部部署的Spring引导应用程序作为Lambda-获取错误

Class not found: com.myapp.app.LambdaHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.myapp.app.LambdaHandler. Current classpath: file:/var/task/

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

我的包在BOOT_INF中->类->com.>myapp->应用程序->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);
} 
}

Application.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);
}
}

我的控制器-

@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;
}
}

无服务器.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

pom.xml

我能知道我做错了什么吗-

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.myapp.app</groupId>
<artifactId>myapp-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> 
<version>1.11.931</version> </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- Spring boot lambda Serverless -->
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-spring</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot2</artifactId>
<version>1.5.2</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.authorize</groupId>
<artifactId>anet-java-sdk</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.226</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
<executable>false</executable>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

您需要配置serverless.yml以指定StreamLambdaHandler的完整包作为handler属性。

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

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

在Springboot中,我们有所有的@Controller、@Service、annotation。因此,Lamba不够聪明,无法理解这一点。除非我们做一些破解。但这对于亚马逊网络服务来说是不可取的。我们能做什么-

我们可以使用Spring cloud函数。这是链接-

  • https://www.baeldung.com/spring-cloud-function

  • https://spring.io/projects/spring-cloud-function

  • https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-aws

如果你喜欢这些信息,请竖起大拇指。

将以下内容添加到pom.xml文件:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.28.RELEASE</version>  // You may need to update this version according to your Java & Spring Boot version
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>

最新更新