MarkLogic POJO数据绑定接口:执行POJO搜索时的JSONMappingException

时间:2017-08-27 11:20:29

标签: java marklogic pojo

我目前正在使用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();

上收到错误

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

另外值得注意的是,你可以在consructor中拥有多个参数,你只需要以Jackson的方式进行。以下是两种方式的示例(带注释和不带注释):https://manosnikolaidis.wordpress.com/2015/08/25/jackson-without-annotations/

我建议使用注释作为杰克逊内置的注释。但是如果你想在没有注释的情况下这样做,那就是代码:

    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();