[英]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.