You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Micronaut Lambda代理集成无法将JSON反序列化为Ping对象

Micronaut Lambda Proxy: POST @Body Deserialization Fails with UnsatisfiedRouteException

Looks like you're hitting a common gotcha with API Gateway Lambda proxy integration—the request structure sent to your Lambda is wrapped in an API Gateway-specific envelope, not the raw HTTP request body your local Micronaut app expects. Let's break down why this happens and how to fix it.

Why This Happens

When you use API Gateway's Lambda proxy integration, API Gateway doesn't pass the raw request directly to your Lambda. Instead, it wraps everything in a JSON object that includes fields like httpMethod, headers, body, and isBase64Encoded. For your POST request, the actual JSON payload you sent ends up as a string inside the body field of this wrapper object.

Your local Micronaut app works because it receives the raw request body, but in Lambda, Micronaut's proxy handler needs to explicitly extract that body field to deserialize it into your Ping object. If this extraction isn't configured correctly, Micronaut can't find the required Ping ping argument, hence the UnsatisfiedRouteException.

Fixes to Try

1. Use the Correct Lambda Handler

Make sure your Lambda is using the right Micronaut proxy handler class for your API Gateway type:

  • For REST API proxy integration: Use io.micronaut.function.aws.proxy.MicronautLambdaHandler
  • For HTTP API proxy integration: Use io.micronaut.function.aws.proxy.HttpApiLambdaHandler

You can set this in your Lambda console's "Handler" field, or define it in your application.yml:

micronaut:
  function:
    aws:
      proxy:
        enabled: true
        handler-class: io.micronaut.function.aws.proxy.MicronautLambdaHandler

2. Check API Gateway's Content Handling Setting

In your API Gateway integration request configuration:

  • Go to your API -> Resources -> Your POST method -> Integration Request
  • Look for the "Content Handling" dropdown
  • Set it to "Convert to text" (not "Convert to binary"). This ensures API Gateway passes the JSON body as a plain string instead of base64-encoding it, which Micronaut can parse correctly.

3. Verify Jackson-Kotlin Module Dependency

Since you're using Kotlin, ensure your build file includes the Jackson-Kotlin module to handle deserialization of Kotlin data classes. For Gradle:

implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.micronaut.jackson:micronaut-jackson-databind")

For Maven:

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
    <groupId>io.micronaut.jackson</groupId>
    <artifactId>micronaut-jackson-databind</artifactId>
</dependency>

This module lets Jackson properly handle Kotlin's data class constructors without requiring a no-arg constructor.

4. Test the Wrapped Request Locally (Optional)

To debug further, you can simulate the API Gateway proxy request locally using a tool like Postman. Send a POST request to your local app with a body matching the API Gateway wrapper format:

{
  "httpMethod": "POST",
  "path": "/ping",
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{\"value\": \"myvalue\"}",
  "isBase64Encoded": false
}

If this works locally, the issue is definitely with your Lambda/API Gateway configuration rather than your code.

Why the GET Request Works

Your GET endpoint doesn't rely on a request body—it only uses a header, which the Micronaut proxy handler correctly extracts from the API Gateway wrapper's headers field. That's why it works in both environments.

内容的提问来源于stack exchange,提问作者codependent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 03:43:21