簡體   English   中英

Micronaut 歸還空的身體

[英]Micronaut returns the empty body

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

有人可以指出我做錯了什么嗎?

PS我使用下一個教程,只是添加了DynamoDB支持: https ://guides.micronaut.io/latest/mn-application-aws-lambda-graalvm-gradle-java.html

我能夠通過使用自定義序列化程序獲得響應正文。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM