如何返回 N1qlQueryResult 作为 Couchbase 数据库的 REST API 的响应?
我想返回 N1qlQueryResult
作为我的 REST API
的响应.下面是代码:
I want to return the N1qlQueryResult
as a response of my REST API
. Below is the code:
@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<?> get() {
List<N1qlQueryRow> result = null;
try {
Cluster cluster = CouchbaseCluster.create("localhost");
Bucket bucket = cluster.openBucket("myBucket", "xyz");
bucket.bucketManager().createN1qlPrimaryIndex(true, false);
N1qlQueryResult queryResult = bucket.query(N1qlQuery.simple("select * FROM myBucket"));
queryResult.forEach(System.out::println);
result = queryResult.allRows();
} catch (final Exception exception) {
exception.printStackTrace();
}
return ResponseEntity.ok(result);
}
我收到错误消息:
无法写入内容:找不到类的序列化程序com.couchbase.client.java.query.DefaultN1qlQueryRow 并且没有属性发现要创建 BeanSerializer(为避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:java.util.ArrayList[0]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:没有序列化程序为类 com.couchbase.client.java.query.DefaultN1qlQueryRow 找到并且没有发现创建 BeanSerializer 的属性(以避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链:java.util.ArrayList[0])
Could not write content: No serializer found for class com.couchbase.client.java.query.DefaultN1qlQueryRow and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.couchbase.client.java.query.DefaultN1qlQueryRow and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])
解决办法是什么?我想以 JSON
的形式返回响应.
What is the solution? I want to return response as JSON
.
推荐答案
ObjectMapper
默认在 bean 为 empty
时失败.我们可以像下面这样禁用它:
ObjectMapper
by default fails when bean is empty
. We can disable this like below:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
但在您的情况下,它并不能解决问题,因为结果无论如何都是空的.返回不代表 POJO
本身的 type
-s 并不是一个好主意.N1qlQueryResult
和 DefaultAsyncN1qlQueryRow
没有经典的 getters
和 Jackson
不能识别类似的方法 value()
默认情况下.我建议创建 Map<String, Object>
并手动构建所需的输出并包含所需的属性:
but in your case it does not solve a problem because result will be empty anyway. There is no good idea to return type
-s which do not represent POJO
itself. N1qlQueryResult
and DefaultAsyncN1qlQueryRow
do not have classic getters
and Jackson
does not recognise methods alike value()
by default. I propose to create Map<String, Object>
and build required output and include needed properties manually:
Map<String, Object> n1QlResult = new HashMap<>();
n1QlResult.put("status", result.status());
// other ...
n1QlResult.put("rows", result.allRows()
.stream()
.map(i -> i.value().toMap())
.collect(Collectors.toList()));
最后返回:
return ResponseEntity.ok(n1QlResult);
当然,当你有很多这样的方法时,你可以编写通用层将这些对象转换为 Map
或编写自定义序列化器并通过 ObjectMapper
用户配置code>Spring 容器.
Of course, when you have many methods like this you can write common layer which translates these kind of object to Map
or write custom serialiser and configure ObjectMapper
user by Spring
container.
编辑
如果您只想返回行返回:
EDIT
In case you want to return only rows return:
result.allRows()
.stream()
.map(i -> i.value().toMap())
.collect(Collectors.toList())
相关文章