<small id='OHxEN'></small><noframes id='OHxEN'>

      • <bdo id='OHxEN'></bdo><ul id='OHxEN'></ul>
      <legend id='OHxEN'><style id='OHxEN'><dir id='OHxEN'><q id='OHxEN'></q></dir></style></legend>

        <tfoot id='OHxEN'></tfoot>
      1. <i id='OHxEN'><tr id='OHxEN'><dt id='OHxEN'><q id='OHxEN'><span id='OHxEN'><b id='OHxEN'><form id='OHxEN'><ins id='OHxEN'></ins><ul id='OHxEN'></ul><sub id='OHxEN'></sub></form><legend id='OHxEN'></legend><bdo id='OHxEN'><pre id='OHxEN'><center id='OHxEN'></center></pre></bdo></b><th id='OHxEN'></th></span></q></dt></tr></i><div id='OHxEN'><tfoot id='OHxEN'></tfoot><dl id='OHxEN'><fieldset id='OHxEN'></fieldset></dl></div>

        如何返回 N1qlQueryResult 作为 Couchbase 数据库的 RE

        时间:2023-09-28

        <small id='hKbus'></small><noframes id='hKbus'>

        <legend id='hKbus'><style id='hKbus'><dir id='hKbus'><q id='hKbus'></q></dir></style></legend>
        1. <tfoot id='hKbus'></tfoot>

            <tbody id='hKbus'></tbody>
          <i id='hKbus'><tr id='hKbus'><dt id='hKbus'><q id='hKbus'><span id='hKbus'><b id='hKbus'><form id='hKbus'><ins id='hKbus'></ins><ul id='hKbus'></ul><sub id='hKbus'></sub></form><legend id='hKbus'></legend><bdo id='hKbus'><pre id='hKbus'><center id='hKbus'></center></pre></bdo></b><th id='hKbus'></th></span></q></dt></tr></i><div id='hKbus'><tfoot id='hKbus'></tfoot><dl id='hKbus'><fieldset id='hKbus'></fieldset></dl></div>
              • <bdo id='hKbus'></bdo><ul id='hKbus'></ul>
                • 本文介绍了如何返回 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 并不是一个好主意.N1qlQueryResultDefaultAsyncN1qlQueryRow 没有经典的 gettersJackson 不能识别类似的方法 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())
                  

                  这篇关于如何返回 N1qlQueryResult 作为 Couchbase 数据库的 REST API 的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 mongodb 集合中查找一些值? 下一篇:MongoDB对同一文档的并发更新不是原子的

                  相关文章

                  最新文章

                • <small id='Nd7N5'></small><noframes id='Nd7N5'>

                  <legend id='Nd7N5'><style id='Nd7N5'><dir id='Nd7N5'><q id='Nd7N5'></q></dir></style></legend>

                  1. <i id='Nd7N5'><tr id='Nd7N5'><dt id='Nd7N5'><q id='Nd7N5'><span id='Nd7N5'><b id='Nd7N5'><form id='Nd7N5'><ins id='Nd7N5'></ins><ul id='Nd7N5'></ul><sub id='Nd7N5'></sub></form><legend id='Nd7N5'></legend><bdo id='Nd7N5'><pre id='Nd7N5'><center id='Nd7N5'></center></pre></bdo></b><th id='Nd7N5'></th></span></q></dt></tr></i><div id='Nd7N5'><tfoot id='Nd7N5'></tfoot><dl id='Nd7N5'><fieldset id='Nd7N5'></fieldset></dl></div>
                    1. <tfoot id='Nd7N5'></tfoot>

                      • <bdo id='Nd7N5'></bdo><ul id='Nd7N5'></ul>