【问题标题】:MarkLogic POJO Data Binding Interface: JSONMappingException when performing a POJO searchMarkLogic POJO 数据绑定接口:执行 POJO 搜索时出现 JSONMappingException
【发布时间】:2017-08-27 11:20:29
【问题描述】:

我目前正在使用 MarkLogic POJO 数据绑定接口。我能够将 POJO 写入 MarkLogic。现在我想搜索那些 POJO 并检索搜索结果。我正在按照以下说明进行操作:https://docs.marklogic.com/guide/java/binding#id_89573 但是,搜索结果似乎没有返回正确的对象。我收到了 JSONMappingException。代码如下:

    HashMap<String, MatchedPropertyInfo> matchedProperties = new HashMap<String, MatchedPropertyInfo>();
    PropertyMatches PM = new PropertyMatches(123,"uri/prefix/location2", "uri/prefix", 1234,0,"/aKey","/aLocation",true,matchedProperties);
    MatchedPropertyInfo MPI1 = new MatchedPropertyInfo("matched/property/uri1", "matched/property/key1", "matched/property/location1", true,"ValueMatch1", 12, 1*1.0/3, true);
    MatchedPropertyInfo MPI2 = new MatchedPropertyInfo("matched/property/uri2", "matched/property/key2", "matched/property/location2", true,"ValueMatch2", 14, 1.0/2.0, true);
    PM.getMatchedProperties().put("matched/property/prefix/location1", MPI1);
    PM.getMatchedProperties().put("matched/property/prefix/location2", MPI2);

    PojoRepository myClassRepo = client.newPojoRepository(PropertyMatches.class, Long.class);
    myClassRepo.write(PM);

    PojoQueryBuilder qb = myClassRepo.getQueryBuilder();
    PojoPage<PropertyMatches> matches = myClassRepo.search(qb.value("uri", "uri/prefix/location2"),1);
    if (matches.hasContent()) {
        while (matches.hasNext()) {
            PropertyMatches aPM = matches.next();
            System.out.println("  " + aPM.getURI());
        }
     } else {
        System.out.println("  No matches");
     }

PropertyMatches (PM) 对象已成功写入 MarkLogic 数据库。此类包含一个成员:private String URI,它以"uri/prefix/location2" 启动。 matches.hasContent() 在上面的示例中返回 true。但是,我在PropertyMatches aPM = matches.next(); 上遇到错误

【问题讨论】:

    标签: java marklogic pojo


    【解决方案1】:

    在 MarkLogic 中搜索 POJO 并将它们读入 Java 程序需要 POJO 有一个空的构造函数。在这种情况下,PropertyMatches 应该有 public PropertyMatches(){} 并且 MatchedPropertyInfo 应该有 public MatchedPropertyInfo(){}

    【讨论】:

      【解决方案2】:

      感谢@sjoerd999 发布您找到的答案。只是为了添加一些文档参考,这里讨论了这个主题:http://docs.marklogic.com/guide/java/binding#id_54408 和这里:https://docs.marklogic.com/javadoc/client/com/marklogic/client/pojo/PojoRepository.html

      另外值得注意的是,你可以在构造函数中有多个参数,你只需要按照杰克逊的方式来做。以下是两种方式的示例(带注释和不带注释):https://manosnikolaidis.wordpress.com/2015/08/25/jackson-without-annotations/

      我建议使用 Jackson 内置的注释。但是如果你想在没有注释的情况下这样做,这里是代码:

          ObjectMapper mapper = new ObjectMapper();
      
          // Avoid having to annotate the Person class
          // Requires Java 8, pass -parameters to javac
          // and jackson-module-parameter-names as a dependency
          mapper.registerModule(new ParameterNamesModule());
      
          // make private fields of Person visible to Jackson
          mapper.setVisibility(FIELD, ANY);
      

      如果您想使用 PojoRepository 执行此操作,则必须使用不受支持的 getObjectMapper 方法来获取 ObjectMapper 并在其上调用 registerModule 和 setVisibility:

          ObjectMapper objectMapper = ((PojoRepositoryImpl) myClassRepo).getObjectMapper();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 2021-05-03
        • 2014-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多