简体   繁体   English

如何返回N1qlQueryResult作为Couchbase数据库的REST API的响应?

[英]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. N1qlQueryResultDefaultAsyncN1qlQueryRow没有经典gettersJackson不承认方法相似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.

相关问题 N1ql Couchbase:如何让N1qlQueryResult成为孩子? - N1ql Couchbase: How to get child with N1qlQueryResult? 如何返回 JSONObject 列表作为 REST API 响应 - How to return a list of JSONObject as REST API response 如何将执行的 GROOVY 脚本结果返回到 REST API 响应 - How to return executed GROOVY script results to REST API Response 如何在 Jackson 框架 rest api 中返回 JSONArray 的响应? - How to return response of JSONArray in Jackson framework rest api's? 当 spring REST api 中缺少 header 时如何返回响应正文 - how to return a response body when header is missing in spring REST api 返回 XML 响应 Rest API 和 Spring - Return XML response Rest API with Spring 如何使 Java Rest API 在处理前半部分后返回响应,然后在返回响应后继续后半部分? - How to make Java Rest API to return response after first half of processing, then continue the second half after return response? 如何返回 Rest API 中的计数 - How to return count in Rest API 如何使用weburl中的REST API控制器返回xls excel文件响应 - How to return xls excel file response using REST API controller in weburl 在Angular 8应用中上传媒体文件时,如何从rest api返回响应? - How to return response from rest api when uploading media file in an Angular 8 app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM