Home » Java » aws lambda does not find the Gson class

aws lambda does not find the Gson class

Posted by: admin October 23, 2018 Leave a comment

Questions:

I was trying aws lambda, I was trying to throw an exception with a message json, so I use the Gson library to build the json.

The problem is that when running it in aws Lambda, it does not find the Gson class of the library. In local, it works correctly.

this is the error thrown by aws lambda:

    "errorMessage": "com/google/gson/Gson",
  "errorType": "java.lang.NoClassDefFoundError",

this is the code:

    public class LambdaExecutor {
    public void handler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LambdaExceptionHandler lambdaExceptionHandler = new LambdaExceptionHandler();
        lambdaExceptionHandler.setId("134");
        lambdaExceptionHandler.setErrorType("Test error");
        lambdaExceptionHandler.setMessage("Error in the TestLambda execution");
        lambdaExceptionHandler.addError("Error into the lambda");
        lambdaExceptionHandler.addError("Because it must fail epically");
        lambdaExceptionHandler.throwException();

        outputStream.write("Is okey :/" .getBytes());
    }
}

and this the pom:

    <?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>aws-lambda</artifactId>
    <version>1.0</version>
    <name>AWS Lambda</name>
    <description>AWS TEST</description>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.0</version>
            <type>jar</type>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.0-M1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>
</project>
Answers: