[英]AWS Lambda function throws ClassNotFoundException - LambdaHandler. Current classpath: file:/var/task/
Spring boot application I am deploying as a Lambda on the top of APIGetway - Getting error我在 APIGetway 顶部作为 Lambda 部署的 Spring Boot 应用程序 - 出现错误
Class not found: com.myapp.app.LambdaHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.myapp.app.LambdaHandler. Current classpath: file:/var/task/
When the Jar file are getting created, I see there are 3 folder - BOOT-INF, META-INF, org.创建 Jar 文件时,我看到有 3 个文件夹 - BOOT-INF、META-INF、org。
My package are there inside the BOOT_INF -> classes -> com -> myapp -> app -> LambdaHandler我的包在 BOOT_INF -> classes -> com -> myapp -> app -> LambdaHandler 里面
Deployment doing using - serverless.yml部署使用 - serverless.yml
Deployment from Command prompt CLI - serverless deploy从命令提示符 CLI 部署 - 无服务器部署
StreamLambdaHandler.java 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应用.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);
}
}
My Controller -我的控制器 -
@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;
}
}
Serverless.yml -无服务器.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 pom.xml
May I Know what I am doing wrong?我可以知道我做错了什么吗? -
-
<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>
You need to configure your serverless.yml to specify the full package of StreamLambdaHandler as the handler attribute.您需要配置您的 serverless.yml 以指定 StreamLambdaHandler 的完整包作为处理程序属性。
For example, if the class was in com.myapp package.例如,如果该类位于 com.myapp 包中。 You would need to specify com.myapp.StreamLambdaHandler
您需要指定 com.myapp.StreamLambdaHandler
You are currently specifying com.myapp.app.LambdaHandler and that is a class that doesn't exist.您当前正在指定 com.myapp.app.LambdaHandler ,这是一个不存在的类。
In Springboot we have all the @Controller, @Service, annotation.在 Springboot 中,我们拥有所有的 @Controller、@Service、注解。 So Lamba is not smart enough to understand this.
所以兰巴不够聪明,无法理解这一点。 Until unless we do some hacks.
除非我们做一些黑客攻击。 But that is not advisable by Amazon Web Services.
但亚马逊网络服务不建议这样做。 What we can do -
我们能做的——
We can make a use of Spring cloud function.我们可以利用 Spring Cloud 功能。 Here is the link -
链接在这里 -
https://www.baeldung.com/spring-cloud-function https://www.baeldung.com/spring-cloud-function
https://spring.io/projects/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 https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-aws
Please put thumbs up, if you like this information.如果您喜欢此信息,请竖起大拇指。
Add below to the pom.xml
file:在
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.