Home > Blockchain >  Java Lambda for spring-cloud version to 3.2.3/3.1.7 org/springframework/boot/ApplicationContextFacto
Java Lambda for spring-cloud version to 3.2.3/3.1.7 org/springframework/boot/ApplicationContextFacto

Time:04-18

I'm trying change version for spring-cloud-function-adapter-aws from 3.0.7.RELEASE to either 3.1.7 or 3.2.3 (as Spring Cloud Function Vulnerability CVE-2022-22963) but getting error as it is not able to find the class

java.lang.NoClassDefFoundError: org/spring framework/boot/ApplicationContextFactory at org.springframework.cloud.function.context.FunctionalSpringApplication.(FunctionalSpringApplication.java:67) at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.springApplication(AbstractSpringFunctionAdapterInitializer.java:378) at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.initialize(AbstractSpringFunctionAdapterInitializer.java:121) at org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler.initialize(SpringBootStreamHandler.java:61) at org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler.handleRequest(SpringBootStreamHandler.java:53) Caused by: java.lang.ClassNotFoundException:

My Application.java

@ComponentScan

@SpringBootApplication public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

}

FunctionConfiguration.java

@Configuration public class FunctionConfiguration { private static Logger logger = LoggerFactory.getLogger(FunctionConfiguration.class);

@Autowired
ActionService service;

@Bean
public Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> containerService() {
    return value -> {
        try {
            APIGatewayProxyResponseEvent responseEvent = checkHttpMethod(value);
            if (responseEvent != null) {
                responseEvent.setBody("Option Method");
                return responseEvent;
            } else {
                return createResponseEvent(value);
            }
        } catch (Exception e) {
            return new APIGatewayProxyResponseEvent().withBody(e.getMessage()).withStatusCode(500)
                    .withHeaders(createResultHeader(value));
        }
    };
}

private APIGatewayProxyResponseEvent checkHttpMethod(APIGatewayProxyRequestEvent event) {
    APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
    if (event.getHttpMethod() != null && event.getHttpMethod().equalsIgnoreCase("options")) {
        responseEvent.setHeaders(createResultHeader(event));
        responseEvent.setStatusCode(200);
        return responseEvent;
    } else
        return null;
}

private APIGatewayProxyResponseEvent createResponseEvent(APIGatewayProxyRequestEvent event) {
    APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();

    try {
        responseEvent = service.actionMethod(event);
        responseEvent.setHeaders(createResultHeader(event));
        return responseEvent;
    } catch (Exception e) {
        logger.error("Error executing method", e);
        responseEvent.setHeaders(createResultHeader(event));
        responseEvent.setBody(e.getMessage());
        responseEvent.setStatusCode(500);
        return responseEvent;
    }

}

private Map<String, String> createResultHeader(APIGatewayProxyRequestEvent event) {
    Map<String, String> resultHeader = new HashMap<>();
    resultHeader.put("Content-Type", "application/json");
    resultHeader.put("Access-Control-Allow-Headers", "Content-Type");
    resultHeader.put("Vary", "Origin");
    
    try {
        String origin = event.getHeaders().get("origin");
        resultHeader.put("Access-Control-Allow-Origin", origin);
    } catch (Exception e) {
        logger.error("origin not exist to add");
    }
    resultHeader.put("Access-Control-Allow-Credentials", "true");
    logger.info(" Headers added ");
    return resultHeader;
}

}

pom.xml

4.0.0 com.app.lambda springBootLambda 1.0.3 springBootLambda

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
    <spring-cloud-function.version>3.0.7.RELEASE
    </spring-cloud-function.version>
    <wrapper.version>1.0.17.RELEASE</wrapper.version>
    <aws-lambda-java-events.version>2.2.7</aws-lambda-java-events.version>
    <aws-java-sdk-s3.version>1.11.792</aws-java-sdk-s3.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-json</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-indexer</artifactId>
        <scope>optional</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-adapter-aws</artifactId>
        <version>${spring-cloud-function.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>${aws-lambda-java-core.version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.672</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-core</artifactId>
        <version>1.11.672</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>${aws-lambda-java-events.version}</version>
    </dependency>
</dependencies>
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <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>${wrapper.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <configuration>
                <finalName>${project.artifactId}-${project.version}</finalName>
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>shaded</shadedClassifierName>
            </configuration>
        </plugin>
    </plugins>
</build>

serverless.yml

service: SpringBoot-Lambda provider: name: aws runtime: java8 region: us-east-1 memory: 2048 timeout: 40

spring:
jpa: hibernate.ddl-auto: update generate-ddl: true show-sql: true

jar file that will be uploaded and executed on AWS

package: artifact: target/{project.artifactId}-${project.version}.jar

#define Lambda function functions: createMethod: handler: org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler events: # api gateway - http: path: pass method: post cors: true environment: # environment variables FUNCTION_NAME: SpringBoot-Lambda

CodePudding user response:

You need to upgrade Spring Boot as well. You are using 2.3.0 and ApplicationContextFactory was added in 2.4, but 2.4.x is no longer supported.

You should upgrade to Spring Boot 2.5.12 or 2.6.6.

  • Related