【问题标题】:Micronaut returns the empty bodyMicronaut 归还空的身体
【发布时间】:2022-06-14 00:19:52
【问题描述】:

在我的“Hello World”原生(GraalVM)AWS Lambda 应用程序中,Micronaut 返回空主体,而不是将地图序列化为 JSON。这是代码

@Controller
public class BookController {

    private static final DynamoDbClient ddb = DynamoDbClient.builder()
            .httpClient(UrlConnectionHttpClient.builder().build()).build();

    @Get("/{id}")
    public Map<String, AttributeValue> getById(@PathVariable String id) {
        GetItemResponse result = ddb.getItem(GetItemRequest.builder()
                .tableName("DemoTable")
                .key(Map.of(
                        "id", AttributeValue.builder().s(id).build()))
                .build());
        
        System.out.println(result.item());

        return result.item();
    }

}

System.out.println(result.item()) 行打印所有数据,但 http 响应不包含这些数据。

回复如下:

{
  "statusCode": 200,
  "multiValueHeaders": {
    "Content-Type": [
      "application/json"
    ],
    "Date": [
      "Mon, 23 May 2022 20:26:13 GMT"
    ]
  },
  "body": "{}",
  "isBase64Encoded": false
}

在我看到的所有示例中,bean 使用注释 @Introspected 进行正确的 JSON 序列化,但 Map 肯定没有。

我尝试扩展一个 HashMap 类来添加注释,但没有结果

@Introspected
public class Asset extends HashMap<String, AttributeValue> {

    public Asset() {}

    public Asset(Map<String, AttributeValue> map) {
        super(map);
    }
}

谁能指出我做错了什么?

附:我用下个教程,刚刚添加了DynamoDB支持:https://guides.micronaut.io/latest/mn-application-aws-lambda-graalvm-gradle-java.html

【问题讨论】:

  • 这是错误。 No serializer found for class software.amazon.awssdk.services.dynamodb.model.AttributeValue and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ... AttributeValue 未序列化。对于旧版本的 DynamoDB 客户端,这不是问题。你可能需要做类似github.com/awsdocs/aws-doc-sdk-examples/issues/…
  • 另外,如果您知道它们有些静态的属性,那么您可以编写自己的映射器类,它可以轻松序列化。类似github.com/aws-samples/serverless-java-frameworks-samples/blob/…

标签: java aws-lambda micronaut graalvm micronaut-aws


【解决方案1】:

我能够通过使用 custom serializer 获得响应正文。

@Controller
class HelloWorldController {
  private static final DynamoDbClient client = DynamoDbClient.builder()
    .httpClient(UrlConnectionHttpClient.builder().build()).build();

  private static final ObjectMapper mapper = new ObjectMapper();

  static {
    SimpleModule module = new SimpleModule();
    module.addSerializer(AttributeValue.class, new AttributeValueSerializer());
    mapper.registerModule(module);
  }

  @Get("/{id}")
  public HttpResponse<?> getById(@PathVariable final String id) throws JsonProcessingException {
    GetItemResponse result = client.getItem(GetItemRequest.builder()
      .tableName("Products")
      .key(Map.of("PK", AttributeValue.builder().s(id).build()))
      .build());

    System.out.println(result.item());
    return HttpResponse.ok(mapper.writeValueAsString(result.item()));
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2019-05-06
    • 2013-07-11
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多