微女归还空体

发布于 2025-01-31 16:38:55 字数 1864 浏览 4 评论 0原文

在我的“ 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
}

在所有示例中,我看到豆类使用注释 @intropsected 进行适当的JSON序列化,但映射绝对没有。

我试图扩展一个hashmap课程来添加注释,但是没有结果,

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

    public Asset() {}

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

有人可以将我指向我做错了什么?

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

In my "Hello World" native(GraalVM) AWS Lambda application Micronaut returns the empty body instead of serializing a map as JSON. Here is the code

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

}

The line System.out.println(result.item()) prints all data but http response does not contain that.

Here is the response:

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

In all examples that I have seen beans use annotation @Introspected for proper JSON serialization but Map definitely does not have it.

I tried to extend a HashMap class for adding the annotation, but without the result

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

    public Asset() {}

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

Can someone point me to what I'm doing wrong?

P.S. I use the next tutorial, just added DynamoDB support: https://guides.micronaut.io/latest/mn-application-aws-lambda-graalvm-gradle-java.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-02-07 16:38:55

我能够通过使用 cutomialiserizer

@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<String> 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()));
  }
}

I was able to get a response body by using a 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<String> 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()));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文