aws-lambdamicronautgraalvm-native-imagemicronaut-awsmicronaut-serde

Adding micronaut serde fails starting application as serverless with lanch class as io.micronaut.function.aws.runtime.MicronautLambdaRuntime


micronaut serde to support serialization and deserialization on GraalVM Native Image. But after adding it the lambda stopped working with below error :

Request loop failed with: Error decoding HTTP response body: No bean introspection available for type [class com.amazonaws.serverless.proxy.model.AwsProxyRequest]. Ensure the class is annotated with io.micronaut.core.annotation.Introspected

Micronaut Version = 3.9.4

I added micronaut serde to support serialization and deserialization on GraalVM Native Image.

 <dependency>
      <groupId>io.micronaut.serde</groupId>
      <artifactId>micronaut-serde-jackson</artifactId>
      <scope>compile</scope>
    </dependency>
<annotationProcessorPaths combine.self="override">
    <path>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.version}</version>
    </path>
    <path>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-processor</artifactId>
      <version>1.4.1.Final</version>
    </path>
    <path>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-inject-java</artifactId>
      <version>${micronaut.version}</version>
    </path>
    <path>
      <groupId>io.micronaut.data</groupId>
      <artifactId>micronaut-data-processor</artifactId>
      <version>${micronaut.data.version}</version>
    </path>
    <path>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-graal</artifactId>
      <version>${micronaut.version}</version>
    </path>
    <path>
      <groupId>io.micronaut.serde</groupId>
      <artifactId>micronaut-serde-processor</artifactId>
      <version>1.5.3</version>
    </path>
    <path>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-http-validation</artifactId>
      <version>${micronaut.version}</version>
    </path>
    <path>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-validation</artifactId>
      <version>${micronaut.version}</version>
    </path>
    <path>
      <groupId>io.micronaut.openapi</groupId>
      <artifactId>micronaut-openapi</artifactId>
      <version>${micronaut.openapi.version}</version>
    </path>
    <path>
      <groupId>io.micronaut.security</groupId>
      <artifactId>micronaut-security-annotations</artifactId>
      <version>${micronaut.security.version}</version>
    </path>
</annotationProcessorPaths>

But after adding it the lambda stopped working with below error :

Request loop failed with: Error decoding HTTP response body: No bean introspection available for type [class com.amazonaws.serverless.proxy.model.AwsProxyRequest]. Ensure the class is annotated with io.micronaut.core.annotation.Introspected

I am using following command to create lambda function

mvn clean package -U -Dpackaging=docker-native -D"exec.mainClass"="io.micronaut.function.aws.runtime.MicronautLambdaRuntime" -D"micronaut.runtime"="lambda" -Pgraalvm -DskipTestsclear

Solution

  • Because com.amazonaws.serverless.proxy.model.AwsProxyRequest is an external class (definition is here), you'll need to import it into your app like this

    @SerdeImport(AwsProxyRequest.class)
    

    In a Java app, this will be done in Application.java like this:

    import io.micronaut.runtime.Micronaut;
    import io.micronaut.serde.annotation.SerdeImport;
    import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
    
    @SerdeImport(AwsProxyRequest.class)
    public class Application {
        public static void main(String[] args) {
            Micronaut.run(Application.class, args);
        }
    }
    

    In a Kotlin app, this will be done in ApplicationKt like this:

    @SerdeImport(AwsProxyRequest::class)
    object MyTestApp // needs an object to annotate
    
    fun main(args: Array<String>) {
        run(*args)
    }
    

    Please note that you may have to import more classes using @SerdeImport until this error is gone.

    It may also be the case that it is not possible to use micronaut-serde-jackson with your app, then you'll have to switch to

    <dependency>
        <groupId>io.micronaut</groupId>
        <artifactId>micronaut-jackson-databind</artifactId>
        <scope>runtime</scope>
    </dependency>
    

    Doc: Enabling Serialization of External Classes