[英]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.在我的“Hello World”本机(GraalVM)AWS Lambda 应用程序中,Micronaut 返回空主体,而不是将地图序列化为 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. System.out.println(result.item())行打印所有数据,但 http 响应不包含该数据。
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.在我看到的所有示例中,bean 使用注释@Introspected进行正确的 JSON 序列化,但 Map 绝对没有它。
I tried to extend a HashMap class for adding the annotation, but without the result我试图扩展一个 HashMap 类来添加注释,但没有结果
@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
PS我使用下一个教程,只是添加了DynamoDB支持: 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()));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.