如何返回N1qlQueryResult作为Couchbase数据库的REST API的响应? [英] How to return N1qlQueryResult as the response of REST API for Couchbase databse?

查看:82
本文介绍了如何返回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失败.我们可以像下面这样禁用它:

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本身,这是一个好主意.默认情况下,N1qlQueryResultDefaultAsyncN1qlQueryRow没有经典的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆