[英]How to return N1qlQueryResult as the response of REST API for Couchbase databse?
I want to return the N1qlQueryResult
as a response of my REST API
. 我想返回
N1qlQueryResult
作为我的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);
}
I get error message: 我收到错误消息:
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]);
无法写入内容:未为com.couchbase.client.java.query.DefaultN1qlQueryRow类找到序列化程序,也未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链: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])
嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:没有为com.couchbase.client.java.query.DefaultN1qlQueryRow类找到序列化程序,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链) :java.util.ArrayList [0])
What is the solution? 解决办法是什么? I want to return response as
JSON
. 我想将响应作为
JSON
返回。
ObjectMapper
by default fails when bean is empty
. 当bean为
empty
时,默认情况下ObjectMapper
失败。 We can disable this like below: 我们可以像下面这样禁用它:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
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. 返回不代表
POJO
本身的-s type
不是好主意。 N1qlQueryResult
and DefaultAsyncN1qlQueryRow
do not have classic getters
and Jackson
does not recognise methods alike value()
by default. N1qlQueryResult
和DefaultAsyncN1qlQueryRow
没有经典getters
和Jackson
不承认方法相似value()
默认情况下。 I propose to create Map<String, Object>
and build required output and include needed properties manually: 我建议创建
Map<String, Object>
并构建所需的输出并手动包含所需的属性:
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()));
and finally return: 最后返回:
return ResponseEntity.ok(n1QlResult);
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. 当然,当您有许多类似这样的方法时,您可以编写将这些对象转换为
Map
公共层,或者编写自定义序列化程序并通过Spring
容器配置ObjectMapper
用户。
EDIT 编辑
In case you want to return only rows return: 如果只想返回行,则返回:
result.allRows()
.stream()
.map(i -> i.value().toMap())
.collect(Collectors.toList())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.