简体   繁体   中英

Micronaut returns the empty body

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM