2017-08-27 73 views
1

我此刻与MarkLogic POJO数据绑定接口工作时JSONMappingException。我可以为MarkLogic编写POJO。现在我想搜索这些POJO并检索搜索结果。我遵循以下指示:https://docs.marklogic.com/guide/java/binding#id_89573但是,搜索结果似乎不会返回正确的对象。我得到一个JSONMappingException。代码如下:MarkLogic POJO数据绑定接口:执行POJO搜索

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数据库。这个类包含一个成员:它与"uri/prefix/location2"开始private String URI。在上面的例子中,matches.hasContent()返回true。但是,我越来越对PropertyMatches aPM = matches.next();

回答

2

搜索的POJO的错误MarkLogic并把它读成Java程序需要的POJO有一个空的构造。在这种情况下PropertyMatches应该有public PropertyMatches(){}和MatchedPropertyInfo应该有public MatchedPropertyInfo(){}

0

谢谢@ sjoerd999发布您找到的答案。我想补充一些文件的引用,这个话题在这里讨论:http://docs.marklogic.com/guide/java/binding#id_54408这里:https://docs.marklogic.com/javadoc/client/com/marklogic/client/pojo/PojoRepository.html

另外值得注意的是,你可以在consructor有多个参数,你就必须做到这一点的杰克逊的方式。这里有两种方法的示例(带注释和不带):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();