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

huangapple go评论79阅读模式
英文:

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

问题

Micronaut serde用于支持GraalVM Native Image上的序列化和反序列化。但在添加后,Lambda出现以下错误:

请求循环失败:解码HTTP响应体时出错:类型[class com.amazonaws.serverless.proxy.model.AwsProxyRequest]没有可用于bean内省。请确保该类已用io.micronaut.core.annotation.Introspected注释。

Micronaut版本 = 3.9.4

我添加了Micronaut serde以支持GraalVM Native Image上的序列化和反序列化。

<dependency>
    <groupId>io.micronaut.serde</groupId>
    <artifactId>micronaut-serde-jackson</artifactId>
    <scope>compile</scope>
</dependency>

以下是我用于创建Lambda函数的命令:

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

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.

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

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&quot;exec.mainClass&quot;=&quot;io.micronaut.function.aws.runtime.MicronautLambdaRuntime&quot; -D&quot;micronaut.runtime&quot;=&quot;lambda&quot; -Pgraalvm -DskipTestsclear

答案1

得分: 2

因为 com.amazonaws.serverless.proxy.model.AwsProxyRequest 是外部类(定义在这里),您需要像这样将其导入到您的应用程序中:

@SerdeImport(AwsProxyRequest.class)

在Java应用程序中,可以在 Application.java 中这样做:

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

在Kotlin应用程序中,可以在 ApplicationKt 中这样做:

@SerdeImport(AwsProxyRequest::class)
object MyTestApp // 需要一个对象来注解

fun main(args: Array<String>) {
    run(*args)
}

请注意,您可能需要使用 @SerdeImport 导入更多的类,直到这个错误消失。

如果无法在您的应用程序中使用 micronaut-serde-jackson,那么您将不得不切换到:

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

文档:启用外部类的序列化

英文:

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&lt;String&gt;) {
	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

&lt;dependency&gt;
    &lt;groupId&gt;io.micronaut&lt;/groupId&gt;
    &lt;artifactId&gt;micronaut-jackson-databind&lt;/artifactId&gt;
    &lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;

Doc: Enabling Serialization of External Classes

答案2

得分: 0

使用Serdable注解来标记AwsProxyRequest类。这样它就可以正常工作。

@Serdable
public class AwsProxyRequest {}
英文:

Annotate class AwsProxyRequest with Serdable. That way it will work.

@Serdeable
public class AwsProxyRequest {}

huangapple
  • 本文由 发表于 2023年7月28日 00:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定