如何返回N1qlQueryResult作为Couchbase数据库的REST API的响应? [英] How to return N1qlQueryResult as the response of REST API for Couchbase databse?
问题描述
我想返回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
失败.我们可以像下面这样禁用它:
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);
但是对于您而言,它不能解决问题,因为结果无论如何都是空的.返回type
-s并不代表POJO
本身,这是一个好主意.默认情况下,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
的公共层,或者编写自定义序列化程序并通过Spring
容器配置ObjectMapper
用户.
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())
这篇关于如何返回N1qlQueryResult作为Couchbase数据库的REST API的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!